xref: /titanic_50/usr/src/cmd/cdrw/misc_scsi.c (revision b36bd5c006a9e77300173b89e361a64db9b1fa94)
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
54730c9c4Smikeri  * Common Development and Distribution License (the "License").
64730c9c4Smikeri  * 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) {
575*b36bd5c0Szk194757 			/*
576*b36bd5c0Szk194757 			 * The check_device() call will issue
577*b36bd5c0Szk194757 			 * a TEST UNIT READY (TUR) and retry many
578*b36bd5c0Szk194757 			 * times when a DVD-R is present. The DKIOCEJECT
579*b36bd5c0Szk194757 			 * ioctl will subsequently fail causing us to
580*b36bd5c0Szk194757 			 * issue the LOAD/UNLOAD SCSI command to the device
581*b36bd5c0Szk194757 			 * with out ejecting the media. Insted of letting
582*b36bd5c0Szk194757 			 * this happen, issue a reset to the device before
583*b36bd5c0Szk194757 			 * issuing the DKIOCEJCET ioctl.
584*b36bd5c0Szk194757 			 */
585*b36bd5c0Szk194757 			if (device_type == DVD_MINUS)
586*b36bd5c0Szk194757 				reset_dev(dev->d_fd);
587*b36bd5c0Szk194757 
5887c478bd9Sstevel@tonic-gate 			if (ioctl(dev->d_fd, DKIOCEJECT, 0) == 0) {
5897c478bd9Sstevel@tonic-gate 				return (1);
5907c478bd9Sstevel@tonic-gate 			}
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 	if (load_unload(dev->d_fd, 0) == 0) {
5947c478bd9Sstevel@tonic-gate 		/* if eject fails */
5957c478bd9Sstevel@tonic-gate 		if ((uscsi_status == 2) && (ASC(rqbuf) == 0x53)) {
5967c478bd9Sstevel@tonic-gate 			/*
5977c478bd9Sstevel@tonic-gate 			 * check that eject is not blocked on the device
5987c478bd9Sstevel@tonic-gate 			 */
5997c478bd9Sstevel@tonic-gate 			if (!prevent_allow_mr(dev->d_fd, 1))
6007c478bd9Sstevel@tonic-gate 				return (0);
6017c478bd9Sstevel@tonic-gate 			return (load_unload(dev->d_fd, 0));
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 		return (0);
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 	return (1);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
60998584592Sarutz  * Get current Read or Write Speed from Mode Page 0x2a.
61098584592Sarutz  *
61198584592Sarutz  * Use the size of the Page to determine which Multimedia Command
61298584592Sarutz  * set (MMC) is present.  Based on the MMC version, get the
61398584592Sarutz  * specified Read/Write Speed.
61498584592Sarutz  *
61598584592Sarutz  * Note that some MMC versions do not necessarily support a
61698584592Sarutz  * (current) Read or Write Speed.  As a result, this function
61798584592Sarutz  * _can_ return a value of zero.
61898584592Sarutz  *
61998584592Sarutz  * The newer standards (reserve and) mark the field(s) as Obsolete,
62098584592Sarutz  * yet many vendors populate the Obsolete fields with valid values
62198584592Sarutz  * (assumedly for backward compatibility).  This is important, as
62298584592Sarutz  * a command like GET PERFORMANCE cannot return _the_ speed; it can
62398584592Sarutz  * only return a Logical-Block-Address-dependent (LBA) speed.  Such
62498584592Sarutz  * values can vary widely between the innermost and outermost Track.
62598584592Sarutz  * Mode Page 0x2a is the best solution identifying "the current
62698584592Sarutz  * (nominal) speed".
6277c478bd9Sstevel@tonic-gate  */
6287c478bd9Sstevel@tonic-gate static uint16_t
62998584592Sarutz cd_speed_get(cd_device *dev, int cmd)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	uchar_t		*mp2a;
63298584592Sarutz 	uint16_t	rate = 0;
63398584592Sarutz 	int		offset;
63498584592Sarutz 	uint_t		buflen = 254;
6357c478bd9Sstevel@tonic-gate 
63698584592Sarutz 	/*
63798584592Sarutz 	 * Allocate a buffer acceptably larger than any nominal
63898584592Sarutz 	 * Page for Page Code 0x2A.
63998584592Sarutz 	 */
64098584592Sarutz 	mp2a = (uchar_t *)my_zalloc(buflen);
64198584592Sarutz 	if (get_mode_page(dev->d_fd, 0x2A, 0, buflen, mp2a) == 0)
64298584592Sarutz 		goto end;
64398584592Sarutz 
64498584592Sarutz 	/* Determine MMC version based on 'Page Length' field */
64598584592Sarutz 	switch (mp2a[1]) {
64698584592Sarutz 	case 0x14:  /* MMC-1 */
64798584592Sarutz 		if (debug)
64898584592Sarutz 			(void) printf("Mode Page 2A: MMC-1\n");
64998584592Sarutz 
65098584592Sarutz 		offset = (cmd == GET_READ_SPEED) ? 14 : 20;
65198584592Sarutz 		rate = read_scsi16(&mp2a[offset]);
65298584592Sarutz 		break;
65398584592Sarutz 
65498584592Sarutz 
65598584592Sarutz 	case 0x18: /* MMC-2 */
65698584592Sarutz 		if (debug)
65798584592Sarutz 			(void) printf("Mode Page 2A: MMC-2;"
65898584592Sarutz 			    " Read and Write Speeds are "
65998584592Sarutz 			    "obsolete\n");
66098584592Sarutz 
66198584592Sarutz 		/* see if "Obsolete" values are valid: */
66298584592Sarutz 		offset = (cmd == GET_READ_SPEED) ? 14 : 20;
66398584592Sarutz 		rate = read_scsi16(&mp2a[offset]);
66498584592Sarutz 		break;
66598584592Sarutz 
66698584592Sarutz 	default: /* MMC-3 or newer */
66798584592Sarutz 		if (debug)
66898584592Sarutz 			(void) printf("Mode Page 2A: MMC-3 or"
66998584592Sarutz 			    " newer; Read Speed is obsolete.\n");
67098584592Sarutz 
6717c478bd9Sstevel@tonic-gate 		if (cmd == GET_READ_SPEED) {
67298584592Sarutz 			/* this is Obsolete, but try it */
67398584592Sarutz 			offset = 14;
67498584592Sarutz 			rate = read_scsi16(&mp2a[offset]);
6757c478bd9Sstevel@tonic-gate 		} else {
67698584592Sarutz 			/* Write Speed is not obsolete */
67798584592Sarutz 			offset = 28;
67898584592Sarutz 			rate = read_scsi16(&mp2a[offset]);
67998584592Sarutz 
68098584592Sarutz 			if (rate == 0) {
68198584592Sarutz 				/*
68298584592Sarutz 				 * then try an Obsolete field
68398584592Sarutz 				 * (but this shouldn't happen!)
68498584592Sarutz 				 */
68598584592Sarutz 				offset = 20;
68698584592Sarutz 				rate = read_scsi16(&mp2a[offset]);
6877c478bd9Sstevel@tonic-gate 			}
6887c478bd9Sstevel@tonic-gate 		}
68998584592Sarutz 		break;
69098584592Sarutz 	}
69198584592Sarutz end:
6927c478bd9Sstevel@tonic-gate 	free(mp2a);
69398584592Sarutz 
69498584592Sarutz 	if (debug)
69598584592Sarutz 		(void) printf("cd_speed_get: %s Speed is "
69698584592Sarutz 		    "%uX\n", (cmd == GET_READ_SPEED) ?
69798584592Sarutz 		    "Read" : "Write", cdrw_bandwidth_to_x(rate));
6987c478bd9Sstevel@tonic-gate 	return (rate);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * CD speed related functions (ioctl style) for drives which do not support
7037c478bd9Sstevel@tonic-gate  * real time streaming.
70498584592Sarutz  *
70598584592Sarutz  * For the SET operations, the SET CD SPEED command needs
70698584592Sarutz  * both the Read Speed and the Write Speed.  Eg, if
70798584592Sarutz  * we're trying to set the Write Speed (SET_WRITE_SPEED),
70898584592Sarutz  * then we first need to obtain the current Read Speed.
70998584592Sarutz  * That speed is specified along with the chosen_speed (the
71098584592Sarutz  * Write Speed in this case) in the SET CD SPEED command.
7117c478bd9Sstevel@tonic-gate  */
7127c478bd9Sstevel@tonic-gate int
7137c478bd9Sstevel@tonic-gate cd_speed_ctrl(cd_device *dev, int cmd, int speed)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	uint16_t rate;
7167c478bd9Sstevel@tonic-gate 
71798584592Sarutz 	switch (cmd) {
71898584592Sarutz 	case GET_READ_SPEED:
71998584592Sarutz 		rate = cd_speed_get(dev, GET_READ_SPEED);
72098584592Sarutz 		return (cdrw_bandwidth_to_x(rate));
72198584592Sarutz 
72298584592Sarutz 	case GET_WRITE_SPEED:
72398584592Sarutz 		rate = cd_speed_get(dev, GET_WRITE_SPEED);
72498584592Sarutz 		return (cdrw_bandwidth_to_x(rate));
72598584592Sarutz 
72698584592Sarutz 	case SET_READ_SPEED:
72798584592Sarutz 		rate = cd_speed_get(dev, GET_WRITE_SPEED);
72898584592Sarutz 		return (set_cd_speed(dev->d_fd,
72998584592Sarutz 		    cdrw_x_to_bandwidth(speed), rate));
73098584592Sarutz 		break;
73198584592Sarutz 
73298584592Sarutz 	case SET_WRITE_SPEED:
73398584592Sarutz 		rate = cd_speed_get(dev, GET_READ_SPEED);
7347c478bd9Sstevel@tonic-gate 		return (set_cd_speed(dev->d_fd, rate,
73598584592Sarutz 		    cdrw_x_to_bandwidth(speed)));
73698584592Sarutz 		break;
73798584592Sarutz 
73898584592Sarutz 	default:
7397c478bd9Sstevel@tonic-gate 		return (0);
7407c478bd9Sstevel@tonic-gate 	}
74198584592Sarutz }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate /*
74498584592Sarutz  * Manage sending of SET STREAMING command using the specified
74598584592Sarutz  * read_speed and write_speed.
74698584592Sarutz  *
74798584592Sarutz  * This function allocates and initializes a Performance
74898584592Sarutz  * Descriptor, which is sent as part of the SET STREAMING
74998584592Sarutz  * command.  The descriptor is deallocated before function
75098584592Sarutz  * exit.
75198584592Sarutz  */
75298584592Sarutz static int
75398584592Sarutz do_set_streaming(cd_device *dev, uint_t read_speed,
75498584592Sarutz 	uint_t write_speed)
75598584592Sarutz {
75698584592Sarutz 	int ret;
75798584592Sarutz 	uchar_t *str;
75898584592Sarutz 
75998584592Sarutz 	/* Allocate and initialize the Performance Descriptor */
76098584592Sarutz 	str = (uchar_t *)my_zalloc(SET_STREAM_DATA_LEN);
76198584592Sarutz 
76298584592Sarutz 	/* Read Time (in milliseconds) */
76398584592Sarutz 	load_scsi32(&str[16], 1000);
76498584592Sarutz 	/* Write Time (in milliseconds) */
76598584592Sarutz 	load_scsi32(&str[24], 1000);
76698584592Sarutz 
76798584592Sarutz 	/* Read Speed */
76898584592Sarutz 	load_scsi32(&str[12], (uint32_t)read_speed);
76998584592Sarutz 	/* Write Speed */
77098584592Sarutz 	load_scsi32(&str[20], (uint32_t)write_speed);
77198584592Sarutz 
77298584592Sarutz 	/* issue SET STREAMING command */
77398584592Sarutz 	ret = set_streaming(dev->d_fd, str);
77498584592Sarutz 	free(str);
77598584592Sarutz 
77698584592Sarutz 	return (ret);
77798584592Sarutz }
77898584592Sarutz 
77998584592Sarutz /*
78098584592Sarutz  * cd speed related functions for drives which support
78198584592Sarutz  * Real-Time Streaming Feature.
78298584592Sarutz  *
78398584592Sarutz  * For the SET operations, the SET STREAMING command needs
78498584592Sarutz  * both the Read Speed and the Write Speed.  Eg, if
78598584592Sarutz  * we're trying to set the Write Speed (SET_WRITE_SPEED),
78698584592Sarutz  * then we first need to obtain the current Read Speed.
78798584592Sarutz  * That speed is specified along with the chosen_speed (the
78898584592Sarutz  * Write Speed in this case) in the SET STREAMING command.
7897c478bd9Sstevel@tonic-gate  */
7907c478bd9Sstevel@tonic-gate int
7917c478bd9Sstevel@tonic-gate rt_streaming_ctrl(cd_device *dev, int cmd, int speed)
7927c478bd9Sstevel@tonic-gate {
79398584592Sarutz 	int ret = 0;
79498584592Sarutz 	uint_t rate;
7957c478bd9Sstevel@tonic-gate 
79698584592Sarutz 	switch (cmd) {
79798584592Sarutz 	case GET_WRITE_SPEED:
79898584592Sarutz 		rate = cd_speed_get(dev, GET_WRITE_SPEED);
79998584592Sarutz 		ret = (int)cdrw_bandwidth_to_x(rate);
80098584592Sarutz 		break;
80198584592Sarutz 
80298584592Sarutz 	case GET_READ_SPEED:
80398584592Sarutz 		rate = cd_speed_get(dev, GET_READ_SPEED);
80498584592Sarutz 		ret = (int)cdrw_bandwidth_to_x(rate);
80598584592Sarutz 		break;
80698584592Sarutz 
80798584592Sarutz 	case SET_READ_SPEED: {
80898584592Sarutz 		uint_t write_speed = cd_speed_get(dev, GET_WRITE_SPEED);
80998584592Sarutz 
81098584592Sarutz 		/* set Read Speed using SET STREAMING */
81198584592Sarutz 		ret = do_set_streaming(dev,
81298584592Sarutz 		    cdrw_x_to_bandwidth(speed), write_speed);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		/* If rt_speed_ctrl fails for any reason use cd_speed_ctrl */
8157c478bd9Sstevel@tonic-gate 		if (ret == 0) {
8167c478bd9Sstevel@tonic-gate 			if (debug)
8177c478bd9Sstevel@tonic-gate 				(void) printf(" real time speed control"
8187c478bd9Sstevel@tonic-gate 				    " failed, using CD speed control\n");
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = cd_speed_ctrl;
8217c478bd9Sstevel@tonic-gate 			ret = dev->d_speed_ctrl(dev, cmd, speed);
8227c478bd9Sstevel@tonic-gate 		}
82398584592Sarutz 		break;
82498584592Sarutz 	}
8257c478bd9Sstevel@tonic-gate 
82698584592Sarutz 	case SET_WRITE_SPEED: {
82798584592Sarutz 		uint_t read_speed = cd_speed_get(dev, GET_READ_SPEED);
82898584592Sarutz 
82998584592Sarutz 		/* set Write Speed using SET STREAMING */
83098584592Sarutz 		ret = do_set_streaming(dev, read_speed,
83198584592Sarutz 		    cdrw_x_to_bandwidth(speed));
83298584592Sarutz 
83398584592Sarutz 		/* If rt_speed_ctrl fails for any reason use cd_speed_ctrl */
83498584592Sarutz 		if (ret == 0) {
83598584592Sarutz 			if (debug)
83698584592Sarutz 				(void) printf(" real time speed control"
83798584592Sarutz 				    " failed, using CD speed control\n");
83898584592Sarutz 
83998584592Sarutz 			dev->d_speed_ctrl = cd_speed_ctrl;
84098584592Sarutz 			ret = dev->d_speed_ctrl(dev, cmd, speed);
84198584592Sarutz 		}
84298584592Sarutz 		break;
84398584592Sarutz 	}
84498584592Sarutz 
84598584592Sarutz 	default:
84698584592Sarutz 		break;
84798584592Sarutz 	}
84898584592Sarutz 
8497c478bd9Sstevel@tonic-gate 	return (ret);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate  * Initialize device for track-at-once mode of writing. All of the data will
8547c478bd9Sstevel@tonic-gate  * need to be written to the track without interruption.
8557c478bd9Sstevel@tonic-gate  * This initialized TAO by setting page code 5 and speed.
8567c478bd9Sstevel@tonic-gate  */
8577c478bd9Sstevel@tonic-gate void
8587c478bd9Sstevel@tonic-gate write_init(int mode)
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate 	(void) printf(gettext("Initializing device"));
8617c478bd9Sstevel@tonic-gate 	if (simulation)
8627c478bd9Sstevel@tonic-gate 		(void) printf(gettext("(Simulation mode)"));
8637c478bd9Sstevel@tonic-gate 	print_n_flush("...");
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	get_media_type(target->d_fd);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/* DVD- requires DAO mode */
8687c478bd9Sstevel@tonic-gate 	if (device_type == DVD_MINUS) {
8697c478bd9Sstevel@tonic-gate 		write_mode = DAO_MODE;
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/* DVD+ and DVD- have no support for AUDIO, bail out */
8737c478bd9Sstevel@tonic-gate 	if ((mode == TRACK_MODE_AUDIO) && (device_type != CD_RW)) {
8747c478bd9Sstevel@tonic-gate 		err_msg(gettext("Audio mode is only supported for CD media\n"));
8757c478bd9Sstevel@tonic-gate 		exit(1);
8767c478bd9Sstevel@tonic-gate 	}
877a2b4fdf6Srameshc 	if (simulation &&
878a2b4fdf6Srameshc 	    check_device(target, CHECK_MEDIA_IS_NOT_BLANK) &&
879a2b4fdf6Srameshc 	    !check_device(target, CHECK_MEDIA_IS_NOT_ERASABLE) &&
880a2b4fdf6Srameshc 	    device_type != DVD_PLUS_W) {
881a2b4fdf6Srameshc 		/*
882a2b4fdf6Srameshc 		 * If we were in simulation mode, and media wasn't blank,
883a2b4fdf6Srameshc 		 * but medium was erasable, then cdrw goes to erase the
884a2b4fdf6Srameshc 		 * contents of the media after the simulation writing in order
885a2b4fdf6Srameshc 		 * to cleanup the ghost TOC (see write_fini() calls blank()).
886a2b4fdf6Srameshc 		 * This is bad because it removes existing data if media was
887a2b4fdf6Srameshc 		 * multi-session. Therefore, we no longer allow simulation
888a2b4fdf6Srameshc 		 * writing if such condition is met. we don't blank the DVD+RW
889a2b4fdf6Srameshc 		 * media, so DVD+RWs are fine.
890a2b4fdf6Srameshc 		 */
891a2b4fdf6Srameshc 		err_msg(gettext(
892a2b4fdf6Srameshc 		    "Cannot perform simulation for non-blank media\n"));
893a2b4fdf6Srameshc 		exit(1);
894a2b4fdf6Srameshc 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if (!prepare_for_write(target, mode, simulation, keep_disc_open)) {
8977c478bd9Sstevel@tonic-gate 		/* l10n_NOTE : 'failed' as in Initializing device...failed  */
8987c478bd9Sstevel@tonic-gate 		(void) printf(gettext("failed.\n"));
8997c478bd9Sstevel@tonic-gate 		err_msg(gettext("Cannot initialize device for write\n"));
9007c478bd9Sstevel@tonic-gate 		exit(1);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Initializing device...done"  */
9037c478bd9Sstevel@tonic-gate 	(void) printf(gettext("done.\n"));
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	/* if speed change option was used (-p) then try to set the speed */
9067c478bd9Sstevel@tonic-gate 	if (requested_speed != 0) {
9077c478bd9Sstevel@tonic-gate 		if (verbose)
9087c478bd9Sstevel@tonic-gate 			(void) printf(gettext("Trying to set speed to %dX.\n"),
9097c478bd9Sstevel@tonic-gate 			    requested_speed);
9107c478bd9Sstevel@tonic-gate 		if (target->d_speed_ctrl(target, SET_WRITE_SPEED,
9117c478bd9Sstevel@tonic-gate 		    requested_speed) == 0) {
9127c478bd9Sstevel@tonic-gate 			err_msg(gettext("Unable to set speed.\n"));
9137c478bd9Sstevel@tonic-gate 			exit(1);
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 		if (verbose) {
9167c478bd9Sstevel@tonic-gate 			int speed;
9177c478bd9Sstevel@tonic-gate 			speed = target->d_speed_ctrl(target,
9187c478bd9Sstevel@tonic-gate 			    GET_WRITE_SPEED, 0);
9197c478bd9Sstevel@tonic-gate 			if (speed == requested_speed) {
9207c478bd9Sstevel@tonic-gate 				(void) printf(gettext("Speed set to %dX.\n"),
9217c478bd9Sstevel@tonic-gate 				    speed);
92298584592Sarutz 			} else if (speed == 0) {
92398584592Sarutz 				(void) printf(gettext("Could not obtain "
92498584592Sarutz 				    "current Write Speed.\n"));
9257c478bd9Sstevel@tonic-gate 			} else {
9267c478bd9Sstevel@tonic-gate 				(void) printf(
9277c478bd9Sstevel@tonic-gate 				gettext("Speed set to closest approximation "
9287c478bd9Sstevel@tonic-gate 				    "of %dX allowed by device (%dX).\n"),
9297c478bd9Sstevel@tonic-gate 				    requested_speed, speed);
9307c478bd9Sstevel@tonic-gate 			}
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate void
9367c478bd9Sstevel@tonic-gate write_fini(void)
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Finalizing (Can take several minutes)..."));
9397c478bd9Sstevel@tonic-gate 	/* Some drives don't like this while in test write mode */
9407c478bd9Sstevel@tonic-gate 	if (!simulation) {
9417c478bd9Sstevel@tonic-gate 		if (!finalize(target)) {
9427c478bd9Sstevel@tonic-gate 			/*
9437c478bd9Sstevel@tonic-gate 			 * It is possible that the drive is busy writing the
9447c478bd9Sstevel@tonic-gate 			 * buffered portion. So do not get upset yet.
9457c478bd9Sstevel@tonic-gate 			 */
9467c478bd9Sstevel@tonic-gate 			(void) sleep(10);
9477c478bd9Sstevel@tonic-gate 			if (!finalize(target)) {
9487c478bd9Sstevel@tonic-gate 				if (debug) {
9497c478bd9Sstevel@tonic-gate 					(void) printf("status %x, %x/%x/%x\n",
9507c478bd9Sstevel@tonic-gate 					    uscsi_status, SENSE_KEY(rqbuf),
9517c478bd9Sstevel@tonic-gate 					    ASC(rqbuf), ASCQ(rqbuf));
9527c478bd9Sstevel@tonic-gate 				}
9537c478bd9Sstevel@tonic-gate 
9545d465cc7Sminht 				/*
9555d465cc7Sminht 				 * Different vendor drives return different
9565d465cc7Sminht 				 * sense error info for CLOSE SESSION command.
9575d465cc7Sminht 				 * The Panasonic drive that we are using is
9585d465cc7Sminht 				 * one such drive.
9595d465cc7Sminht 				 */
9605d465cc7Sminht 				if (device_type == DVD_MINUS) {
9617c478bd9Sstevel@tonic-gate 					if (verbose) {
9627c478bd9Sstevel@tonic-gate 						(void) printf(
9637c478bd9Sstevel@tonic-gate 						    "skipping finalizing\n");
9647c478bd9Sstevel@tonic-gate 					}
9657c478bd9Sstevel@tonic-gate 				} else {
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 			/* l10n_NOTE : 'failed' as in finishing up...failed  */
9687c478bd9Sstevel@tonic-gate 					(void) printf(gettext("failed.\n"));
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 					err_msg(gettext(
9717c478bd9Sstevel@tonic-gate 					    "Could not finalize the disc.\n"));
9727c478bd9Sstevel@tonic-gate 					exit(1);
9737c478bd9Sstevel@tonic-gate 				}
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 			}
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 		if (vol_running) {
9797c478bd9Sstevel@tonic-gate 			(void) eject_media(target);
9807c478bd9Sstevel@tonic-gate 		}
9817c478bd9Sstevel@tonic-gate 	} else if (check_device(target, CHECK_MEDIA_IS_NOT_BLANK)) {
9827c478bd9Sstevel@tonic-gate 		/*
9837c478bd9Sstevel@tonic-gate 		 * Some drives such as the pioneer A04 will retain a
9847c478bd9Sstevel@tonic-gate 		 * ghost TOC after a simulation write is done. The
9857c478bd9Sstevel@tonic-gate 		 * media will actually be blank, but the drive will
9867c478bd9Sstevel@tonic-gate 		 * report a TOC. There is currently no other way to
9877c478bd9Sstevel@tonic-gate 		 * re-initialize the media other than ejecting or
9887c478bd9Sstevel@tonic-gate 		 * to ask the drive to clear the leadout. The laser
9897c478bd9Sstevel@tonic-gate 		 * is currently off so nothing is written to the
9907c478bd9Sstevel@tonic-gate 		 * media (on a good behaving drive).
9917c478bd9Sstevel@tonic-gate 		 * NOTE that a device reset does not work to make
9927c478bd9Sstevel@tonic-gate 		 * the drive re-initialize the media.
9937c478bd9Sstevel@tonic-gate 		 */
9947c478bd9Sstevel@tonic-gate 
995a2b4fdf6Srameshc 		blanking_type = "clear_ghost";
9967c478bd9Sstevel@tonic-gate 		blank();
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Finishing up...done"  */
10007c478bd9Sstevel@tonic-gate 	(void) printf(gettext("done.\n"));
10017c478bd9Sstevel@tonic-gate }
1002