xref: /titanic_52/usr/src/cmd/cdrw/misc_scsi.c (revision a2b4fdf6f9770a7725bfd41e7b3a92253e26645c)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*a2b4fdf6Srameshc  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <sys/time.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "mmc.h"
407c478bd9Sstevel@tonic-gate #include "util.h"
417c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
427c478bd9Sstevel@tonic-gate #include "transport.h"
437c478bd9Sstevel@tonic-gate #include "main.h"
447c478bd9Sstevel@tonic-gate #include "toshiba.h"
457c478bd9Sstevel@tonic-gate #include "msgs.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 		} else {
3587c478bd9Sstevel@tonic-gate 			buf[3] = 5;	/* always 5 for DVD */
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		if (keep_disc_open) {
3627c478bd9Sstevel@tonic-gate 			buf[3] |= 0xc0;		/* Allow more sessions */
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		/* Select track type (audio or data) */
3667c478bd9Sstevel@tonic-gate 		if (track_mode == TRACK_MODE_DATA) {
3677c478bd9Sstevel@tonic-gate 			buf[4] = 8;		/* 2048 byte sector */
3687c478bd9Sstevel@tonic-gate 		} else {
3697c478bd9Sstevel@tonic-gate 			buf[4] = 0;		/* 2352 byte sector */
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 		buf[7] = buf[8] = 0;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		/* Need to clear these fields for setting into DAO */
3747c478bd9Sstevel@tonic-gate 		if (write_mode == DAO_MODE)
3757c478bd9Sstevel@tonic-gate 			buf[5] = buf[15] = 0;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		/* print out mode for detailed log */
3787c478bd9Sstevel@tonic-gate 		if (debug && verbose) {
3797c478bd9Sstevel@tonic-gate 			int i;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 			(void) printf("setting = [ ");
3827c478bd9Sstevel@tonic-gate 			for (i = 0; i < 15; i++)
3837c478bd9Sstevel@tonic-gate 				(void) printf("0x%x ", buf[i]);
3847c478bd9Sstevel@tonic-gate 			(void) printf("]\n");
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		no_err = set_mode_page(dev->d_fd, buf);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		if (no_err && reset_device) {
3907c478bd9Sstevel@tonic-gate 			/* Turn the test write bit back on */
3917c478bd9Sstevel@tonic-gate 			buf[2] |= 0x10;
3927c478bd9Sstevel@tonic-gate 			no_err = set_mode_page(dev->d_fd, buf);
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		/*
3967c478bd9Sstevel@tonic-gate 		 * Since BUFE is the only optional flag we are
3977c478bd9Sstevel@tonic-gate 		 * setting we will try to turn it off if the command
3987c478bd9Sstevel@tonic-gate 		 * fails.
3997c478bd9Sstevel@tonic-gate 		 */
4007c478bd9Sstevel@tonic-gate 		if (!no_err) {
4017c478bd9Sstevel@tonic-gate 			/*
4027c478bd9Sstevel@tonic-gate 			 * Some old drives may not support HW
4037c478bd9Sstevel@tonic-gate 			 * buffer underrun protection, try again
4047c478bd9Sstevel@tonic-gate 			 * after turning it off.
4057c478bd9Sstevel@tonic-gate 			 */
4067c478bd9Sstevel@tonic-gate 			if (debug)
4077c478bd9Sstevel@tonic-gate 				(void) printf("Turning off BUFE\n");
4087c478bd9Sstevel@tonic-gate 			buf[2] &= ~0x40;
4097c478bd9Sstevel@tonic-gate 			no_err = set_mode_page(dev->d_fd, buf);
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	free(buf);
4147c478bd9Sstevel@tonic-gate 	return (no_err);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate  * Close session. This will write TOC.
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate int
4217c478bd9Sstevel@tonic-gate finalize(cd_device *dev)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	uchar_t *di;
4247c478bd9Sstevel@tonic-gate 	int count, ret, err;
4257c478bd9Sstevel@tonic-gate 	int immediate;
4267c478bd9Sstevel@tonic-gate 	int finalize_max;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/*
4297c478bd9Sstevel@tonic-gate 	 * For ATAPI devices we will use the immediate mode and will
4307c478bd9Sstevel@tonic-gate 	 * poll the command for completion so that this command may
4317c478bd9Sstevel@tonic-gate 	 * not hog the channel. But for SCSI, we will use the treditional
4327c478bd9Sstevel@tonic-gate 	 * way of issuing the command with a large enough timeout. This
4337c478bd9Sstevel@tonic-gate 	 * is done because immediate mode was designed for ATAPI and some
4347c478bd9Sstevel@tonic-gate 	 * SCSI RW drives might not be even tested with it.
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	if ((dev->d_inq[2] & 7) != 0) {
4377c478bd9Sstevel@tonic-gate 		/* SCSI device */
4387c478bd9Sstevel@tonic-gate 		immediate = 0;
4397c478bd9Sstevel@tonic-gate 	} else {
4407c478bd9Sstevel@tonic-gate 		/* non-SCSI (e.g ATAPI) device */
4417c478bd9Sstevel@tonic-gate 		immediate = 1;
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/* We need to close track before close session */
4457c478bd9Sstevel@tonic-gate 	if (device_type == DVD_PLUS) {
4467c478bd9Sstevel@tonic-gate 		if (!close_track(dev->d_fd, 0, 0, immediate))
4477c478bd9Sstevel@tonic-gate 			return (0);
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	if (!close_track(dev->d_fd, 0, 1, immediate)) {
4517c478bd9Sstevel@tonic-gate 		/*
4527c478bd9Sstevel@tonic-gate 		 * For DVD-RW close track is not well defined
4537c478bd9Sstevel@tonic-gate 		 * some drives dont like it, others want us
4547c478bd9Sstevel@tonic-gate 		 * to close track before closing the session.
4557c478bd9Sstevel@tonic-gate 		 * NOTE that for MMC specification it is not mandatory
4567c478bd9Sstevel@tonic-gate 		 * to support close track.
4577c478bd9Sstevel@tonic-gate 		 */
4587c478bd9Sstevel@tonic-gate 		if (device_type == DVD_MINUS) {
4597c478bd9Sstevel@tonic-gate 			if (!close_track(dev->d_fd, 1, 0, immediate)) {
4607c478bd9Sstevel@tonic-gate 				return (0);
4617c478bd9Sstevel@tonic-gate 			} else {
4627c478bd9Sstevel@tonic-gate 				/* command is already done */
4637c478bd9Sstevel@tonic-gate 				if (!immediate)
4647c478bd9Sstevel@tonic-gate 					return (1);
4657c478bd9Sstevel@tonic-gate 			}
4667c478bd9Sstevel@tonic-gate 		} else {
4677c478bd9Sstevel@tonic-gate 			return (0);
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 	} else {
4707c478bd9Sstevel@tonic-gate 		if (!immediate)
4717c478bd9Sstevel@tonic-gate 			return (1);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 	if (immediate) {
4747c478bd9Sstevel@tonic-gate 		(void) sleep(10);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
4777c478bd9Sstevel@tonic-gate 		err = 0;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		if (device_type == CD_RW) {
4807c478bd9Sstevel@tonic-gate 			/* Finalization should not take more than 6 minutes */
4817c478bd9Sstevel@tonic-gate 			finalize_max = FINALIZE_TIMEOUT;
4827c478bd9Sstevel@tonic-gate 		} else {
4837c478bd9Sstevel@tonic-gate 			/* some DVD-RW drives take longer than 6 minutes */
4847c478bd9Sstevel@tonic-gate 			finalize_max = FINALIZE_TIMEOUT*2;
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		for (count = 0; count < finalize_max; count++) {
4887c478bd9Sstevel@tonic-gate 			ret = read_disc_info(dev->d_fd, di);
4897c478bd9Sstevel@tonic-gate 			if (ret != 0)
4907c478bd9Sstevel@tonic-gate 				break;
4917c478bd9Sstevel@tonic-gate 			if (uscsi_status != 2)
4927c478bd9Sstevel@tonic-gate 				err = 1;
4937c478bd9Sstevel@tonic-gate 			if (SENSE_KEY(rqbuf) == 2) {
4947c478bd9Sstevel@tonic-gate 				/* not ready but not becoming ready */
4957c478bd9Sstevel@tonic-gate 				if (ASC(rqbuf) != 4)
4967c478bd9Sstevel@tonic-gate 					err = 1;
4977c478bd9Sstevel@tonic-gate 			} else if (SENSE_KEY(rqbuf) == 5) {
4987c478bd9Sstevel@tonic-gate 				/* illegal mode for this track */
4997c478bd9Sstevel@tonic-gate 				if (ASC(rqbuf) != 0x64)
5007c478bd9Sstevel@tonic-gate 					err = 1;
5017c478bd9Sstevel@tonic-gate 			} else {
5027c478bd9Sstevel@tonic-gate 				err = 1;
5037c478bd9Sstevel@tonic-gate 			}
5047c478bd9Sstevel@tonic-gate 			if (err == 1) {
5057c478bd9Sstevel@tonic-gate 				if (debug) {
5067c478bd9Sstevel@tonic-gate 					(void) printf("Finalization failed\n");
5077c478bd9Sstevel@tonic-gate 					(void) printf("%x %x %x %x\n",
5087c478bd9Sstevel@tonic-gate 					    uscsi_status, SENSE_KEY(rqbuf),
5097c478bd9Sstevel@tonic-gate 					    ASC(rqbuf), ASCQ(rqbuf));
5107c478bd9Sstevel@tonic-gate 				}
5117c478bd9Sstevel@tonic-gate 				free(di);
5127c478bd9Sstevel@tonic-gate 				return (0);
5137c478bd9Sstevel@tonic-gate 			}
5147c478bd9Sstevel@tonic-gate 			if (uscsi_status == 2) {
5157c478bd9Sstevel@tonic-gate 				int i;
5167c478bd9Sstevel@tonic-gate 				/* illegal field in command packet */
5177c478bd9Sstevel@tonic-gate 				if (ASC(rqbuf) == 0x24) {
5187c478bd9Sstevel@tonic-gate 					/* print it out! */
5197c478bd9Sstevel@tonic-gate 					(void) printf("\n");
5207c478bd9Sstevel@tonic-gate 					for (i = 0; i < 18; i++)
5217c478bd9Sstevel@tonic-gate 						(void) printf("%x ",
5227c478bd9Sstevel@tonic-gate 						    (unsigned)(rqbuf[i]));
5237c478bd9Sstevel@tonic-gate 					(void) printf("\n");
5247c478bd9Sstevel@tonic-gate 				}
5257c478bd9Sstevel@tonic-gate 			}
5267c478bd9Sstevel@tonic-gate 			(void) sleep(5);
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		free(di);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	return (ret);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate  * Find out media capacity.
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate int
5377c478bd9Sstevel@tonic-gate get_last_possible_lba(cd_device *dev)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	uchar_t *di;
5407c478bd9Sstevel@tonic-gate 	int cap;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
5437c478bd9Sstevel@tonic-gate 	if (!read_disc_info(dev->d_fd, di)) {
5447c478bd9Sstevel@tonic-gate 		free(di);
5457c478bd9Sstevel@tonic-gate 		return (0);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 	if ((di[21] != 0) && (di[21] != 0xff)) {
5487c478bd9Sstevel@tonic-gate 		cap = ((di[21] * 60) + di[22]) * 75;
5497c478bd9Sstevel@tonic-gate 	} else {
5507c478bd9Sstevel@tonic-gate 		cap = 0;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	free(di);
5547c478bd9Sstevel@tonic-gate 	return (cap);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate int
5587c478bd9Sstevel@tonic-gate read_audio_through_read_cd(cd_device *dev, uint_t start_lba, uint_t nblks,
5597c478bd9Sstevel@tonic-gate     uchar_t *buf)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	int retry;
5627c478bd9Sstevel@tonic-gate 	int ret;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	for (retry = 0; retry < 3; retry++) {
5657c478bd9Sstevel@tonic-gate 		ret = read_cd(dev->d_fd, (uint32_t)start_lba, (uint16_t)nblks,
5667c478bd9Sstevel@tonic-gate 		    1, buf, (uint32_t)(nblks * 2352));
5677c478bd9Sstevel@tonic-gate 		if (ret)
5687c478bd9Sstevel@tonic-gate 			break;
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 	return (ret);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate int
5747c478bd9Sstevel@tonic-gate eject_media(cd_device *dev)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	if (vol_running) {
5777c478bd9Sstevel@tonic-gate 		/* If there is a media, try using DKIOCEJECT 1st */
5787c478bd9Sstevel@tonic-gate 		if (check_device(dev, CHECK_NO_MEDIA) == 0) {
5797c478bd9Sstevel@tonic-gate 			if (ioctl(dev->d_fd, DKIOCEJECT, 0) == 0) {
5807c478bd9Sstevel@tonic-gate 				return (1);
5817c478bd9Sstevel@tonic-gate 			}
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	if (load_unload(dev->d_fd, 0) == 0) {
5857c478bd9Sstevel@tonic-gate 		/* if eject fails */
5867c478bd9Sstevel@tonic-gate 		if ((uscsi_status == 2) && (ASC(rqbuf) == 0x53)) {
5877c478bd9Sstevel@tonic-gate 			/*
5887c478bd9Sstevel@tonic-gate 			 * check that eject is not blocked on the device
5897c478bd9Sstevel@tonic-gate 			 */
5907c478bd9Sstevel@tonic-gate 			if (!prevent_allow_mr(dev->d_fd, 1))
5917c478bd9Sstevel@tonic-gate 				return (0);
5927c478bd9Sstevel@tonic-gate 			return (load_unload(dev->d_fd, 0));
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 		return (0);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 	return (1);
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate /*
6007c478bd9Sstevel@tonic-gate  * Get CD speed from Page code 2A. since GET PERFORMANCE is not supported
6017c478bd9Sstevel@tonic-gate  * (which is already checked before) this mode page *will* have the speed.
6027c478bd9Sstevel@tonic-gate  */
6037c478bd9Sstevel@tonic-gate static uint16_t
6047c478bd9Sstevel@tonic-gate i_cd_speed_read(cd_device *dev, int cmd)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	uchar_t *mp2a;
6077c478bd9Sstevel@tonic-gate 	uint16_t rate;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	mp2a = (uchar_t *)my_zalloc(PAGE_CODE_2A_SIZE);
6107c478bd9Sstevel@tonic-gate 	if (get_mode_page(dev->d_fd, 0x2A, 0, PAGE_CODE_2A_SIZE,
6117c478bd9Sstevel@tonic-gate 	    mp2a) == 0) {
6127c478bd9Sstevel@tonic-gate 		rate = 0;
6137c478bd9Sstevel@tonic-gate 	} else {
6147c478bd9Sstevel@tonic-gate 		if (cmd == GET_READ_SPEED) {
6157c478bd9Sstevel@tonic-gate 			rate = ((uint16_t)mp2a[14] << 8) | mp2a[15];
6167c478bd9Sstevel@tonic-gate 		} else {
6177c478bd9Sstevel@tonic-gate 			rate = ((uint16_t)mp2a[20] << 8) | mp2a[21];
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	free(mp2a);
6217c478bd9Sstevel@tonic-gate 	return (rate);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * CD speed related functions (ioctl style) for drives which do not support
6267c478bd9Sstevel@tonic-gate  * real time streaming.
6277c478bd9Sstevel@tonic-gate  */
6287c478bd9Sstevel@tonic-gate int
6297c478bd9Sstevel@tonic-gate cd_speed_ctrl(cd_device *dev, int cmd, int speed)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	uint16_t rate;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	if ((cmd == GET_READ_SPEED) || (cmd == GET_WRITE_SPEED))
6347c478bd9Sstevel@tonic-gate 		return (XFER_RATE_TO_SPEED(i_cd_speed_read(dev, cmd)));
6357c478bd9Sstevel@tonic-gate 	if (cmd == SET_READ_SPEED) {
6367c478bd9Sstevel@tonic-gate 		rate = i_cd_speed_read(dev, GET_WRITE_SPEED);
6377c478bd9Sstevel@tonic-gate 		return (set_cd_speed(dev->d_fd, SPEED_TO_XFER_RATE(speed),
6387c478bd9Sstevel@tonic-gate 		    rate));
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 	if (cmd == SET_WRITE_SPEED) {
6417c478bd9Sstevel@tonic-gate 		rate = i_cd_speed_read(dev, GET_READ_SPEED);
6427c478bd9Sstevel@tonic-gate 		return (set_cd_speed(dev->d_fd, rate,
6437c478bd9Sstevel@tonic-gate 		    SPEED_TO_XFER_RATE(speed)));
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	return (0);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate  * cd speed related functions for drives which support RT-streaming
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate int
6527c478bd9Sstevel@tonic-gate rt_streaming_ctrl(cd_device *dev, int cmd, int speed)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate 	uchar_t *perf, *str;
6557c478bd9Sstevel@tonic-gate 	int write_perf;
6567c478bd9Sstevel@tonic-gate 	int ret;
6577c478bd9Sstevel@tonic-gate 	uint16_t perf_got;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	write_perf = 0;
6607c478bd9Sstevel@tonic-gate 	if ((cmd == GET_WRITE_SPEED) || (cmd == SET_READ_SPEED))
6617c478bd9Sstevel@tonic-gate 		write_perf = 1;
6627c478bd9Sstevel@tonic-gate 	perf = (uchar_t *)my_zalloc(GET_PERF_DATA_LEN);
6637c478bd9Sstevel@tonic-gate 	if (!get_performance(dev->d_fd, write_perf, perf)) {
6647c478bd9Sstevel@tonic-gate 		ret = 0;
6657c478bd9Sstevel@tonic-gate 		goto end_rsc;
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 	perf_got = (uint16_t)read_scsi32(&perf[20]);
6687c478bd9Sstevel@tonic-gate 	if ((cmd == GET_READ_SPEED) || (cmd == GET_WRITE_SPEED)) {
6697c478bd9Sstevel@tonic-gate 		ret = XFER_RATE_TO_SPEED(perf_got);
6707c478bd9Sstevel@tonic-gate 		goto end_rsc;
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	str = (uchar_t *)my_zalloc(SET_STREAM_DATA_LEN);
6737c478bd9Sstevel@tonic-gate 	(void) memcpy(&str[8], &perf[16], 4);
6747c478bd9Sstevel@tonic-gate 	load_scsi32(&str[16], 1000);
6757c478bd9Sstevel@tonic-gate 	load_scsi32(&str[24], 1000);
6767c478bd9Sstevel@tonic-gate 	if (cmd == SET_WRITE_SPEED) {
6777c478bd9Sstevel@tonic-gate 		load_scsi32(&str[12], (uint32_t)perf_got);
6787c478bd9Sstevel@tonic-gate 		load_scsi32(&str[20], (uint32_t)SPEED_TO_XFER_RATE(speed));
6797c478bd9Sstevel@tonic-gate 	} else {
6807c478bd9Sstevel@tonic-gate 		load_scsi32(&str[20], (uint32_t)perf_got);
6817c478bd9Sstevel@tonic-gate 		load_scsi32(&str[12], (uint32_t)SPEED_TO_XFER_RATE(speed));
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 	ret = set_streaming(dev->d_fd, str);
6847c478bd9Sstevel@tonic-gate 	free(str);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/* If rt_speed_ctrl fails for any reason use cd_speed_ctrl */
6877c478bd9Sstevel@tonic-gate 	if (ret == 0) {
6887c478bd9Sstevel@tonic-gate 		if (debug)
6897c478bd9Sstevel@tonic-gate 			(void) printf(" real time speed control"
6907c478bd9Sstevel@tonic-gate 			    " failed, using CD speed control\n");
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 		dev->d_speed_ctrl = cd_speed_ctrl;
6937c478bd9Sstevel@tonic-gate 		ret = dev->d_speed_ctrl(dev, cmd, speed);
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate end_rsc:
6977c478bd9Sstevel@tonic-gate 	free(perf);
6987c478bd9Sstevel@tonic-gate 	return (ret);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * Initialize device for track-at-once mode of writing. All of the data will
7037c478bd9Sstevel@tonic-gate  * need to be written to the track without interruption.
7047c478bd9Sstevel@tonic-gate  * This initialized TAO by setting page code 5 and speed.
7057c478bd9Sstevel@tonic-gate  */
7067c478bd9Sstevel@tonic-gate void
7077c478bd9Sstevel@tonic-gate write_init(int mode)
7087c478bd9Sstevel@tonic-gate {
7097c478bd9Sstevel@tonic-gate 	(void) printf(gettext("Initializing device"));
7107c478bd9Sstevel@tonic-gate 	if (simulation)
7117c478bd9Sstevel@tonic-gate 		(void) printf(gettext("(Simulation mode)"));
7127c478bd9Sstevel@tonic-gate 	print_n_flush("...");
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	get_media_type(target->d_fd);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/* DVD- requires DAO mode */
7177c478bd9Sstevel@tonic-gate 	if (device_type == DVD_MINUS) {
7187c478bd9Sstevel@tonic-gate 		write_mode = DAO_MODE;
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	/* For debug, print out device config information */
7227c478bd9Sstevel@tonic-gate 	if (debug) {
7237c478bd9Sstevel@tonic-gate 		int i;
7247c478bd9Sstevel@tonic-gate 		uchar_t cap[80];
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		if (get_configuration(target->d_fd, 0, 80, cap))
7277c478bd9Sstevel@tonic-gate 			(void) printf("Drive profile = ");
7287c478bd9Sstevel@tonic-gate 			for (i = 10; i < 70; i += 8)
7297c478bd9Sstevel@tonic-gate 				(void) printf(" 0x%x", cap[i]);
7307c478bd9Sstevel@tonic-gate 			(void) printf("\n");
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	/* DVD+ and DVD- have no support for AUDIO, bail out */
7347c478bd9Sstevel@tonic-gate 	if ((mode == TRACK_MODE_AUDIO) && (device_type != CD_RW)) {
7357c478bd9Sstevel@tonic-gate 		err_msg(gettext("Audio mode is only supported for CD media\n"));
7367c478bd9Sstevel@tonic-gate 		exit(1);
7377c478bd9Sstevel@tonic-gate 	}
738*a2b4fdf6Srameshc 	if (simulation &&
739*a2b4fdf6Srameshc 	    check_device(target, CHECK_MEDIA_IS_NOT_BLANK) &&
740*a2b4fdf6Srameshc 	    !check_device(target, CHECK_MEDIA_IS_NOT_ERASABLE) &&
741*a2b4fdf6Srameshc 	    device_type != DVD_PLUS_W) {
742*a2b4fdf6Srameshc 		/*
743*a2b4fdf6Srameshc 		 * If we were in simulation mode, and media wasn't blank,
744*a2b4fdf6Srameshc 		 * but medium was erasable, then cdrw goes to erase the
745*a2b4fdf6Srameshc 		 * contents of the media after the simulation writing in order
746*a2b4fdf6Srameshc 		 * to cleanup the ghost TOC (see write_fini() calls blank()).
747*a2b4fdf6Srameshc 		 * This is bad because it removes existing data if media was
748*a2b4fdf6Srameshc 		 * multi-session. Therefore, we no longer allow simulation
749*a2b4fdf6Srameshc 		 * writing if such condition is met. we don't blank the DVD+RW
750*a2b4fdf6Srameshc 		 * media, so DVD+RWs are fine.
751*a2b4fdf6Srameshc 		 */
752*a2b4fdf6Srameshc 		err_msg(gettext(
753*a2b4fdf6Srameshc 		    "Cannot perform simulation for non-blank media\n"));
754*a2b4fdf6Srameshc 		exit(1);
755*a2b4fdf6Srameshc 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (!prepare_for_write(target, mode, simulation, keep_disc_open)) {
7587c478bd9Sstevel@tonic-gate 		/* l10n_NOTE : 'failed' as in Initializing device...failed  */
7597c478bd9Sstevel@tonic-gate 		(void) printf(gettext("failed.\n"));
7607c478bd9Sstevel@tonic-gate 		err_msg(gettext("Cannot initialize device for write\n"));
7617c478bd9Sstevel@tonic-gate 		exit(1);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Initializing device...done"  */
7647c478bd9Sstevel@tonic-gate 	(void) printf(gettext("done.\n"));
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	/* if speed change option was used (-p) then try to set the speed */
7677c478bd9Sstevel@tonic-gate 	if (requested_speed != 0) {
7687c478bd9Sstevel@tonic-gate 		if (verbose)
7697c478bd9Sstevel@tonic-gate 			(void) printf(gettext("Trying to set speed to %dX.\n"),
7707c478bd9Sstevel@tonic-gate 			    requested_speed);
7717c478bd9Sstevel@tonic-gate 		if (target->d_speed_ctrl(target, SET_WRITE_SPEED,
7727c478bd9Sstevel@tonic-gate 		    requested_speed) == 0) {
7737c478bd9Sstevel@tonic-gate 			err_msg(gettext("Unable to set speed.\n"));
7747c478bd9Sstevel@tonic-gate 			exit(1);
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 		if (verbose) {
7777c478bd9Sstevel@tonic-gate 			int speed;
7787c478bd9Sstevel@tonic-gate 			speed = target->d_speed_ctrl(target,
7797c478bd9Sstevel@tonic-gate 			    GET_WRITE_SPEED, 0);
7807c478bd9Sstevel@tonic-gate 			if (speed == requested_speed) {
7817c478bd9Sstevel@tonic-gate 				(void) printf(gettext("Speed set to %dX.\n"),
7827c478bd9Sstevel@tonic-gate 				    speed);
7837c478bd9Sstevel@tonic-gate 			} else {
7847c478bd9Sstevel@tonic-gate 				(void) printf(
7857c478bd9Sstevel@tonic-gate 				gettext("Speed set to closest approximation "
7867c478bd9Sstevel@tonic-gate 				    "of %dX allowed by device (%dX).\n"),
7877c478bd9Sstevel@tonic-gate 				    requested_speed, speed);
7887c478bd9Sstevel@tonic-gate 			}
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate void
7947c478bd9Sstevel@tonic-gate write_fini(void)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Finalizing (Can take several minutes)..."));
7977c478bd9Sstevel@tonic-gate 	/* Some drives don't like this while in test write mode */
7987c478bd9Sstevel@tonic-gate 	if (!simulation) {
7997c478bd9Sstevel@tonic-gate 		if (!finalize(target)) {
8007c478bd9Sstevel@tonic-gate 			/*
8017c478bd9Sstevel@tonic-gate 			 * It is possible that the drive is busy writing the
8027c478bd9Sstevel@tonic-gate 			 * buffered portion. So do not get upset yet.
8037c478bd9Sstevel@tonic-gate 			 */
8047c478bd9Sstevel@tonic-gate 			(void) sleep(10);
8057c478bd9Sstevel@tonic-gate 			if (!finalize(target)) {
8067c478bd9Sstevel@tonic-gate 				if (debug) {
8077c478bd9Sstevel@tonic-gate 					(void) printf("status %x, %x/%x/%x\n",
8087c478bd9Sstevel@tonic-gate 					    uscsi_status, SENSE_KEY(rqbuf),
8097c478bd9Sstevel@tonic-gate 					    ASC(rqbuf), ASCQ(rqbuf));
8107c478bd9Sstevel@tonic-gate 				}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 				if ((device_type == DVD_MINUS) &&
8137c478bd9Sstevel@tonic-gate 				    (SENSE_KEY(rqbuf) == 5)) {
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 					if (verbose) {
8167c478bd9Sstevel@tonic-gate 						(void) printf(
8177c478bd9Sstevel@tonic-gate 						    "skipping finalizing\n");
8187c478bd9Sstevel@tonic-gate 					}
8197c478bd9Sstevel@tonic-gate 				} else {
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 			/* l10n_NOTE : 'failed' as in finishing up...failed  */
8227c478bd9Sstevel@tonic-gate 					(void) printf(gettext("failed.\n"));
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 					err_msg(gettext(
8257c478bd9Sstevel@tonic-gate 					    "Could not finalize the disc.\n"));
8267c478bd9Sstevel@tonic-gate 					exit(1);
8277c478bd9Sstevel@tonic-gate 				}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 			}
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 		if (vol_running) {
8337c478bd9Sstevel@tonic-gate 			(void) eject_media(target);
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 	} else if (check_device(target, CHECK_MEDIA_IS_NOT_BLANK)) {
8367c478bd9Sstevel@tonic-gate 		/*
8377c478bd9Sstevel@tonic-gate 		 * Some drives such as the pioneer A04 will retain a
8387c478bd9Sstevel@tonic-gate 		 * ghost TOC after a simulation write is done. The
8397c478bd9Sstevel@tonic-gate 		 * media will actually be blank, but the drive will
8407c478bd9Sstevel@tonic-gate 		 * report a TOC. There is currently no other way to
8417c478bd9Sstevel@tonic-gate 		 * re-initialize the media other than ejecting or
8427c478bd9Sstevel@tonic-gate 		 * to ask the drive to clear the leadout. The laser
8437c478bd9Sstevel@tonic-gate 		 * is currently off so nothing is written to the
8447c478bd9Sstevel@tonic-gate 		 * media (on a good behaving drive).
8457c478bd9Sstevel@tonic-gate 		 * NOTE that a device reset does not work to make
8467c478bd9Sstevel@tonic-gate 		 * the drive re-initialize the media.
8477c478bd9Sstevel@tonic-gate 		 */
8487c478bd9Sstevel@tonic-gate 
849*a2b4fdf6Srameshc 		blanking_type = "clear_ghost";
8507c478bd9Sstevel@tonic-gate 		blank();
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Finishing up...done"  */
8547c478bd9Sstevel@tonic-gate 	(void) printf(gettext("done.\n"));
8557c478bd9Sstevel@tonic-gate }
856