xref: /titanic_44/usr/src/cmd/cdrw/device.c (revision 61ee869ef3fff08996d6710c5d4f7a17bc81415c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*61ee869eSrameshc  * Common Development and Distribution License (the "License").
6*61ee869eSrameshc  * 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 /*
22*61ee869eSrameshc  * Copyright 2007 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 <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <volmgt.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <dirent.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "transport.h"
427c478bd9Sstevel@tonic-gate #include "mmc.h"
437c478bd9Sstevel@tonic-gate #include "device.h"
447c478bd9Sstevel@tonic-gate #include "util.h"
457c478bd9Sstevel@tonic-gate #include "msgs.h"
467c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
477c478bd9Sstevel@tonic-gate #include "toshiba.h"
487c478bd9Sstevel@tonic-gate #include "main.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Old sun drives have a vendor specific mode page for setting/getting speed.
527c478bd9Sstevel@tonic-gate  * Also they use a different method for extracting audio.
537c478bd9Sstevel@tonic-gate  * We have the device inquiry strings at this time. This is used to enable
547c478bd9Sstevel@tonic-gate  * us to use older sun drives to extract audio.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate static int
577c478bd9Sstevel@tonic-gate is_old_sun_drive(cd_device *dev)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	/*
607c478bd9Sstevel@tonic-gate 	 * If we have a SONY CDU 561, CDU 8012, or TOSHIBA model with XMa we
617c478bd9Sstevel@tonic-gate 	 * need to handle these drives a bit differently.
627c478bd9Sstevel@tonic-gate 	 */
637c478bd9Sstevel@tonic-gate 	if (strncmp("SONY", (const char *)&dev->d_inq[8], 4) == 0) {
647c478bd9Sstevel@tonic-gate 		if (strncmp("CDU 561", (const char *)&dev->d_inq[16], 7) == 0)
657c478bd9Sstevel@tonic-gate 			return (1);
667c478bd9Sstevel@tonic-gate 		if (strncmp("CDU-8012", (const char *)&dev->d_inq[16], 8) == 0)
677c478bd9Sstevel@tonic-gate 			return (1);
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if ((strncmp("TOSHIBA", (const char *)&dev->d_inq[8], 7) == 0) &&
717c478bd9Sstevel@tonic-gate 	    (strncmp("XM", (const char *)&dev->d_inq[16], 2) == 0)) {
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 		char product_id[17];
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		/* Changing speed is not allowed for 32X TOSHIBA drives */
767c478bd9Sstevel@tonic-gate 		if (strncmp("SUN32XCD", (const char *)&dev->d_inq[24], 8) == 0)
777c478bd9Sstevel@tonic-gate 			dev->d_cap |= DEV_CAP_SETTING_SPEED_NOT_ALLOWED;
787c478bd9Sstevel@tonic-gate 		(void) strncpy(product_id, (const char *)&dev->d_inq[16], 16);
797c478bd9Sstevel@tonic-gate 		product_id[16] = 0;
807c478bd9Sstevel@tonic-gate 		if (strstr(product_id, "SUN") != NULL)
817c478bd9Sstevel@tonic-gate 			return (1);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	return (0);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * returns a cd_device handle for a node returned by lookup_device()
887c478bd9Sstevel@tonic-gate  * also takes the user supplied name and stores it inside the node
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate cd_device *
917c478bd9Sstevel@tonic-gate get_device(char *user_supplied, char *node)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	cd_device *dev;
947c478bd9Sstevel@tonic-gate 	int fd;
957c478bd9Sstevel@tonic-gate 	uchar_t *cap;
967c478bd9Sstevel@tonic-gate 	char devnode[PATH_MAX];
977c478bd9Sstevel@tonic-gate 	int size;
987c478bd9Sstevel@tonic-gate 	struct dk_minfo mediainfo;
997c478bd9Sstevel@tonic-gate 	int use_cd_speed = 0;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * we need to resolve any link paths to avoid fake files
1037c478bd9Sstevel@tonic-gate 	 * such as /dev/rdsk/../../export/file.
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("get_device(%s, %s)\n", user_supplied ?
1077c478bd9Sstevel@tonic-gate 	    user_supplied : "<nil>", node ? node : "<nil>"));
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	size = resolvepath(node, devnode, PATH_MAX);
1107c478bd9Sstevel@tonic-gate 	if ((size <= 0) || (size >= (PATH_MAX - 1)))
1117c478bd9Sstevel@tonic-gate 		return (NULL);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/* resolvepath may not return a null terminated string */
1147c478bd9Sstevel@tonic-gate 	devnode[size] = '\0';
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/* the device node must be in /devices/ or /vol/dev/rdsk */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if ((strncmp(devnode, "/devices/", 9) != 0) &&
1207c478bd9Sstevel@tonic-gate 	    (strncmp(devnode, "/vol/dev/rdsk", 13) != 0))
1217c478bd9Sstevel@tonic-gate 		return (NULL);
1227c478bd9Sstevel@tonic-gate 	/*
1237c478bd9Sstevel@tonic-gate 	 * Since we are currently running with the user euid it is
1247c478bd9Sstevel@tonic-gate 	 * safe to try to open the file without checking access.
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	fd = open(devnode, O_RDONLY|O_NDELAY);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (fd < 0) {
1307c478bd9Sstevel@tonic-gate 		TRACE(traceall_msg("Cannot open %s: %s\n", node,
1317c478bd9Sstevel@tonic-gate 		    strerror(errno)));
1327c478bd9Sstevel@tonic-gate 		return (NULL);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	dev = (cd_device *)my_zalloc(sizeof (cd_device));
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	dev->d_node = (char *)my_zalloc(strlen(devnode) + 1);
1387c478bd9Sstevel@tonic-gate 	(void) strcpy(dev->d_node, devnode);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	dev->d_fd = fd;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	dev->d_inq = (uchar_t *)my_zalloc(INQUIRY_DATA_LENGTH);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (!inquiry(fd, dev->d_inq)) {
1457c478bd9Sstevel@tonic-gate 		TRACE(traceall_msg("Inquiry failed on device %s\n", node));
1467c478bd9Sstevel@tonic-gate 		if (debug) {
1477c478bd9Sstevel@tonic-gate 			(void) printf("USCSI ioctl failed %d\n",
1487c478bd9Sstevel@tonic-gate 			    uscsi_error);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		free(dev->d_inq);
1517c478bd9Sstevel@tonic-gate 		free(dev->d_node);
1527c478bd9Sstevel@tonic-gate 		(void) close(dev->d_fd);
1537c478bd9Sstevel@tonic-gate 		free(dev);
1547c478bd9Sstevel@tonic-gate 		return (NULL);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (debug) {
1587c478bd9Sstevel@tonic-gate 		cap = (uchar_t *)my_zalloc(18);
1597c478bd9Sstevel@tonic-gate 		(void) printf("Checking device type\n");
1607c478bd9Sstevel@tonic-gate 		if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
1617c478bd9Sstevel@tonic-gate 			if (cap[2] & 0x10)
1627c478bd9Sstevel@tonic-gate 				(void) printf("DVD-R read support\n");
1637c478bd9Sstevel@tonic-gate 			if (cap[3] & 0x10)
1647c478bd9Sstevel@tonic-gate 				(void) printf("DVD-R write support\n");
1657c478bd9Sstevel@tonic-gate 			if (cap[5] & 0x4)
1667c478bd9Sstevel@tonic-gate 				(void) printf("R-W supported\n");
1677c478bd9Sstevel@tonic-gate 			if (cap[2] & 0x20)
1687c478bd9Sstevel@tonic-gate 				(void) printf("DVD-RAM read supported\n");
1697c478bd9Sstevel@tonic-gate 			if (cap[3] & 0x20)
1707c478bd9Sstevel@tonic-gate 				(void) printf("DVD-RAM write supported\n");
1717c478bd9Sstevel@tonic-gate 		} else {
1727c478bd9Sstevel@tonic-gate 			(void) printf("Could not read mode page 2A! \n");
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 		free(cap);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* Detect if it's a Lite-ON drive with a streaming CD problem */
1787c478bd9Sstevel@tonic-gate 	if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
1797c478bd9Sstevel@tonic-gate 	    (strncmp("LTR-48", (const char *)&dev->d_inq[16], 6) == 0)) {
1807c478bd9Sstevel@tonic-gate 		use_cd_speed = 1;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1834becbc5bSrameshc 	/*
1844becbc5bSrameshc 	 * a workaround for the firmware problem in LITE-ON COMBO drives.
1854becbc5bSrameshc 	 * streaming for these drives sets it only to max speed regardless
1864becbc5bSrameshc 	 * of requested speed. cd_speed_ctrl allow speeds less than max
1874becbc5bSrameshc 	 * to be set but not max thus the code below. (x48 is max speed
1884becbc5bSrameshc 	 * for these drives).
1894becbc5bSrameshc 	 */
1904becbc5bSrameshc 	if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
1914becbc5bSrameshc 	    (strncmp("COMBO SOHC-4836VS",
1924becbc5bSrameshc 	    (const char *)&dev->d_inq[16], 17) == 0))
1934becbc5bSrameshc 		if (requested_speed < 48)
1944becbc5bSrameshc 			use_cd_speed = 1;
1954becbc5bSrameshc 
1967c478bd9Sstevel@tonic-gate 	cap = (uchar_t *)my_zalloc(8);
1977c478bd9Sstevel@tonic-gate 	if (is_old_sun_drive(dev)) {
1987c478bd9Sstevel@tonic-gate 		dev->d_read_audio = toshiba_read_audio;
1997c478bd9Sstevel@tonic-gate 		dev->d_speed_ctrl = toshiba_speed_ctrl;
2007c478bd9Sstevel@tonic-gate 	} else {
2017c478bd9Sstevel@tonic-gate 		/*
202c9be8e69Sec158148 		 * If the CD Read Feature is supported, READ CD will work
203c9be8e69Sec158148 		 * and will return jitter free audio data. Otherwise, look
204c9be8e69Sec158148 		 * at Page Code 2A for this information.
2057c478bd9Sstevel@tonic-gate 		 */
206c9be8e69Sec158148 		if (ftr_supported(fd, MMC_FTR_CD_READ) == 1) {
2077c478bd9Sstevel@tonic-gate 			dev->d_read_audio = read_audio_through_read_cd;
2087c478bd9Sstevel@tonic-gate 			dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
2097c478bd9Sstevel@tonic-gate 		} else if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
2107c478bd9Sstevel@tonic-gate 			if (cap[5] & 1) {
2117c478bd9Sstevel@tonic-gate 				dev->d_read_audio = read_audio_through_read_cd;
2127c478bd9Sstevel@tonic-gate 				if (cap[5] & 2)
2137c478bd9Sstevel@tonic-gate 					dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 		/*
217c9be8e69Sec158148 		 * If the Real Time Streaming Feature is supported then
218*61ee869eSrameshc 		 * Real-time streaming commands can be used for speed control
219*61ee869eSrameshc 		 * (except when we want to use cd_speed_ctrl explicitly which
220*61ee869eSrameshc 		 * is specified by setting use_cd_speed to 1).
221c9be8e69Sec158148 		 * Otherwise try SET CD SPEED.
2227c478bd9Sstevel@tonic-gate 		 */
223*61ee869eSrameshc 		if ((ftr_supported(fd, MMC_FTR_RT_STREAM) == 1) &&
224*61ee869eSrameshc 		    !use_cd_speed) {
2257c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = rt_streaming_ctrl;
2267c478bd9Sstevel@tonic-gate 			if (debug)
2277c478bd9Sstevel@tonic-gate 				(void) printf("using rt speed ctrl\n");
2287c478bd9Sstevel@tonic-gate 		} else {
2297c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = cd_speed_ctrl;
2307c478bd9Sstevel@tonic-gate 			if (debug)
2317c478bd9Sstevel@tonic-gate 				(void) printf("using cd speed ctrl\n");
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	if (dev->d_read_audio != NULL)
2357c478bd9Sstevel@tonic-gate 		dev->d_cap |= DEV_CAP_EXTRACT_CDDA;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	dev->d_blksize = 0;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Find the block size of the device so we can translate
2417c478bd9Sstevel@tonic-gate 	 * the reads/writes to the device blocksize.
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGMEDIAINFO, &mediainfo) < 0) {
2457c478bd9Sstevel@tonic-gate 		/*
2467c478bd9Sstevel@tonic-gate 		 * If DKIOCGMEDIAINFO fails we'll try to get
2477c478bd9Sstevel@tonic-gate 		 * the blocksize from the device itself.
2487c478bd9Sstevel@tonic-gate 		 */
2497c478bd9Sstevel@tonic-gate 		if (debug)
2507c478bd9Sstevel@tonic-gate 			(void) printf("DKIOCGMEDIAINFO failed\n");
2517c478bd9Sstevel@tonic-gate 		if (read_capacity(fd, cap))
2527c478bd9Sstevel@tonic-gate 			dev->d_blksize = read_scsi32(cap + 4);
2537c478bd9Sstevel@tonic-gate 	} else {
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		dev->d_blksize = mediainfo.dki_lbsize;
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (debug) {
2597c478bd9Sstevel@tonic-gate 		uint_t bsize;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		(void) printf("blocksize = %d\n", dev->d_blksize);
2627c478bd9Sstevel@tonic-gate 		(void) printf("read_format_capacity = %d \n",
2637c478bd9Sstevel@tonic-gate 		    read_format_capacity(fd, &bsize));
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Some devices will return invalid blocksizes. ie. Toshiba
2687c478bd9Sstevel@tonic-gate 	 * drives will return 2352 when an audio CD is inserted.
2697c478bd9Sstevel@tonic-gate 	 * Older Sun drives will use 512 byte block sizes. All newer
2707c478bd9Sstevel@tonic-gate 	 * drives should have 2k blocksizes.
2717c478bd9Sstevel@tonic-gate 	 */
2727c478bd9Sstevel@tonic-gate 	if (((dev->d_blksize != 512) && (dev->d_blksize != 2048))) {
2737c478bd9Sstevel@tonic-gate 			if (is_old_sun_drive(dev)) {
2747c478bd9Sstevel@tonic-gate 				dev->d_blksize = 512;
2757c478bd9Sstevel@tonic-gate 			} else {
2767c478bd9Sstevel@tonic-gate 				dev->d_blksize = 2048;
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 		if (debug)
2797c478bd9Sstevel@tonic-gate 			(void) printf(" switching to %d\n", dev->d_blksize);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	free(cap);
2837c478bd9Sstevel@tonic-gate 	if (user_supplied) {
2847c478bd9Sstevel@tonic-gate 		dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
2857c478bd9Sstevel@tonic-gate 		(void) strcpy(dev->d_name, user_supplied);
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("Got device %s\n", node));
2887c478bd9Sstevel@tonic-gate 	return (dev);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate void
2927c478bd9Sstevel@tonic-gate fini_device(cd_device *dev)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	free(dev->d_inq);
2957c478bd9Sstevel@tonic-gate 	free(dev->d_node);
2967c478bd9Sstevel@tonic-gate 	(void) close(dev->d_fd);
2977c478bd9Sstevel@tonic-gate 	if (dev->d_name)
2987c478bd9Sstevel@tonic-gate 		free(dev->d_name);
2997c478bd9Sstevel@tonic-gate 	free(dev);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate static int
3037c478bd9Sstevel@tonic-gate vol_name_to_dev_node(char *vname, char *found)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	struct stat statbuf;
3067c478bd9Sstevel@tonic-gate 	char *p1;
3077c478bd9Sstevel@tonic-gate 	int i;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (vname == NULL)
3107c478bd9Sstevel@tonic-gate 		return (0);
3117c478bd9Sstevel@tonic-gate 	if (vol_running)
3127c478bd9Sstevel@tonic-gate 		(void) volmgt_check(vname);
3137c478bd9Sstevel@tonic-gate 	p1 = media_findname(vname);
3147c478bd9Sstevel@tonic-gate 	if (p1 == NULL)
3157c478bd9Sstevel@tonic-gate 		return (0);
3167c478bd9Sstevel@tonic-gate 	if (stat(p1, &statbuf) < 0) {
3177c478bd9Sstevel@tonic-gate 		free(p1);
3187c478bd9Sstevel@tonic-gate 		return (0);
3197c478bd9Sstevel@tonic-gate 	}
3204bc0a2efScasper 	if (S_ISDIR(statbuf.st_mode)) {
3217c478bd9Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
3227c478bd9Sstevel@tonic-gate 			(void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
3237c478bd9Sstevel@tonic-gate 			if (access(found, F_OK) >= 0)
3247c478bd9Sstevel@tonic-gate 				break;
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		if (i == 16) {
3277c478bd9Sstevel@tonic-gate 			free(p1);
3287c478bd9Sstevel@tonic-gate 			return (0);
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 	} else {
3317c478bd9Sstevel@tonic-gate 		(void) strlcpy(found, p1, PATH_MAX);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 	free(p1);
3347c478bd9Sstevel@tonic-gate 	return (1);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * Searches for volume manager's equivalent char device for the
3397c478bd9Sstevel@tonic-gate  * supplied pathname which is of the form of /dev/rdsk/cxtxdxsx
3407c478bd9Sstevel@tonic-gate  */
3417c478bd9Sstevel@tonic-gate static int
3427c478bd9Sstevel@tonic-gate vol_lookup(char *supplied, char *found)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	char tmpstr[PATH_MAX], tmpstr1[PATH_MAX], *p;
3457c478bd9Sstevel@tonic-gate 	int i, ret;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpstr, supplied, PATH_MAX);
3487c478bd9Sstevel@tonic-gate 	if ((p = volmgt_symname(tmpstr)) == NULL) {
3497c478bd9Sstevel@tonic-gate 		if (strrchr(tmpstr, 's') == NULL)
3507c478bd9Sstevel@tonic-gate 			return (0);
3517c478bd9Sstevel@tonic-gate 		*((char *)(strrchr(tmpstr, 's') + 1)) = 0;
3527c478bd9Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
3537c478bd9Sstevel@tonic-gate 			(void) snprintf(tmpstr1, PATH_MAX, "%s%d", tmpstr, i);
3547c478bd9Sstevel@tonic-gate 			if ((p = volmgt_symname(tmpstr1)) != NULL)
3557c478bd9Sstevel@tonic-gate 				break;
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 		if (p == NULL)
3587c478bd9Sstevel@tonic-gate 			return (0);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	ret = vol_name_to_dev_node(p, found);
3617c478bd9Sstevel@tonic-gate 	free(p);
3627c478bd9Sstevel@tonic-gate 	return (ret);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * Builds an open()able device path from a user supplied node which can be
3677c478bd9Sstevel@tonic-gate  * of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
3687c478bd9Sstevel@tonic-gate  * cdrom[n]
3697c478bd9Sstevel@tonic-gate  * returns the path found in 'found' and returns 1. Otherwise returns 0.
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate int
3727c478bd9Sstevel@tonic-gate lookup_device(char *supplied, char *found)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	struct stat statbuf;
3757c478bd9Sstevel@tonic-gate 	int fd;
3767c478bd9Sstevel@tonic-gate 	char tmpstr[PATH_MAX];
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* If everything is fine and proper, no need to analyze */
3794bc0a2efScasper 	if ((stat(supplied, &statbuf) == 0) && S_ISCHR(statbuf.st_mode) &&
3807c478bd9Sstevel@tonic-gate 	    ((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
3817c478bd9Sstevel@tonic-gate 		(void) close(fd);
3827c478bd9Sstevel@tonic-gate 		(void) strlcpy(found, supplied, PATH_MAX);
3837c478bd9Sstevel@tonic-gate 		return (1);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 	if (strncmp(supplied, "/dev/rdsk/", 10) == 0)
3867c478bd9Sstevel@tonic-gate 		return (vol_lookup(supplied, found));
3877c478bd9Sstevel@tonic-gate 	if (strncmp(supplied, "/dev/dsk/", 9) == 0) {
3887c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
3897c478bd9Sstevel@tonic-gate 		    (char *)strrchr(supplied, '/'));
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
3927c478bd9Sstevel@tonic-gate 			(void) close(fd);
3937c478bd9Sstevel@tonic-gate 			(void) strlcpy(found, supplied, PATH_MAX);
3947c478bd9Sstevel@tonic-gate 			return (1);
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 		if ((access(tmpstr, F_OK) == 0) && vol_running)
3977c478bd9Sstevel@tonic-gate 			return (vol_lookup(tmpstr, found));
3987c478bd9Sstevel@tonic-gate 		else
3997c478bd9Sstevel@tonic-gate 			return (0);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 	if ((strncmp(supplied, "cdrom", 5) != 0) &&
4027c478bd9Sstevel@tonic-gate 	    (strlen(supplied) < 32)) {
4037c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
4047c478bd9Sstevel@tonic-gate 		    supplied);
4057c478bd9Sstevel@tonic-gate 		if (access(tmpstr, F_OK) < 0) {
4067c478bd9Sstevel@tonic-gate 			(void) strcat(tmpstr, "s2");
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
4097c478bd9Sstevel@tonic-gate 			(void) close(fd);
4107c478bd9Sstevel@tonic-gate 			(void) strlcpy(found, tmpstr, PATH_MAX);
4117c478bd9Sstevel@tonic-gate 			return (1);
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 		if ((access(tmpstr, F_OK) == 0) && vol_running)
4147c478bd9Sstevel@tonic-gate 			return (vol_lookup(tmpstr, found));
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	return (vol_name_to_dev_node(supplied, found));
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * Opens the device node name passed and returns 1 (true) if the
4217c478bd9Sstevel@tonic-gate  * device is a CD.
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate static int
4257c478bd9Sstevel@tonic-gate is_cd(char *node)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	int fd;
4287c478bd9Sstevel@tonic-gate 	struct dk_cinfo cinfo;
4297c478bd9Sstevel@tonic-gate 	int ret = 1;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	fd = open(node, O_RDONLY|O_NDELAY);
4327c478bd9Sstevel@tonic-gate 	if (fd < 0) {
4337c478bd9Sstevel@tonic-gate 		ret = 0;
4347c478bd9Sstevel@tonic-gate 	} else if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
4357c478bd9Sstevel@tonic-gate 		ret = 0;
4367c478bd9Sstevel@tonic-gate 	} else if (cinfo.dki_ctype != DKC_CDROM) {
4377c478bd9Sstevel@tonic-gate 		ret = 0;
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
4417c478bd9Sstevel@tonic-gate 		(void) close(fd);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 	return (ret);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate static void
4477c478bd9Sstevel@tonic-gate print_header(void)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : Column spacing should be kept same */
4507c478bd9Sstevel@tonic-gate 	(void) printf(gettext("    Node	           Connected Device"));
4517c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : Column spacing should be kept same */
4527c478bd9Sstevel@tonic-gate 	(void) printf(gettext("	           Device type\n"));
4537c478bd9Sstevel@tonic-gate 	(void) printf(
4547c478bd9Sstevel@tonic-gate 	    "----------------------+--------------------------------");
4557c478bd9Sstevel@tonic-gate 	(void) printf("+-----------------\n");
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate  * returns the number of writers or CD/DVD-roms found and the path of
4607c478bd9Sstevel@tonic-gate  * the first device found depending on the mode argument.
4617c478bd9Sstevel@tonic-gate  * possible mode values are:
4627c478bd9Sstevel@tonic-gate  * SCAN_ALL_CDS 	Scan all CD/DVD devices. Return first CD-RW found.
4637c478bd9Sstevel@tonic-gate  * SCAN_WRITERS		Scan all CD-RW devices. Return first one found.
4647c478bd9Sstevel@tonic-gate  * SCAN_LISTDEVS	List all devices found.
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate int
4677c478bd9Sstevel@tonic-gate scan_for_cd_device(int mode, cd_device **found)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	DIR *dir;
4707c478bd9Sstevel@tonic-gate 	struct dirent *dirent;
4717c478bd9Sstevel@tonic-gate 	char sdev[PATH_MAX], dev[PATH_MAX];
4727c478bd9Sstevel@tonic-gate 	cd_device *t_dev;
4737c478bd9Sstevel@tonic-gate 	int writers_found = 0;
4747c478bd9Sstevel@tonic-gate 	int header_printed = 0;
4757c478bd9Sstevel@tonic-gate 	int is_writer;
4767c478bd9Sstevel@tonic-gate 	int retry = 0;
4777c478bd9Sstevel@tonic-gate 	int total_devices_found;
4787c478bd9Sstevel@tonic-gate 	int total_devices_found_last_time = 0;
4797c478bd9Sstevel@tonic-gate 	int defer = 0;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate #define	MAX_RETRIES_FOR_SCANNING 5
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode));
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (mode) {
4867c478bd9Sstevel@tonic-gate 		if (vol_running)
4877c478bd9Sstevel@tonic-gate 			defer = 1;
4887c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Looking for CD devices...\n"));
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate try_again:
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	dir = opendir("/dev/rdsk");
4937c478bd9Sstevel@tonic-gate 	if (dir == NULL)
4947c478bd9Sstevel@tonic-gate 		return (0);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	writers_found = 0;
4977c478bd9Sstevel@tonic-gate 	total_devices_found = 0;
4987c478bd9Sstevel@tonic-gate 	while ((dirent = readdir(dir)) != NULL) {
4997c478bd9Sstevel@tonic-gate 		if (dirent->d_name[0] == '.')
5007c478bd9Sstevel@tonic-gate 			continue;
5017c478bd9Sstevel@tonic-gate 		(void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
5027c478bd9Sstevel@tonic-gate 		    dirent->d_name);
5037c478bd9Sstevel@tonic-gate 		if (strcmp("s2", (char *)strrchr(sdev, 's')) != 0)
5047c478bd9Sstevel@tonic-gate 			continue;
5057c478bd9Sstevel@tonic-gate 		if (!lookup_device(sdev, dev))
5067c478bd9Sstevel@tonic-gate 			continue;
5077c478bd9Sstevel@tonic-gate 		if (!is_cd(dev))
5087c478bd9Sstevel@tonic-gate 			continue;
5097c478bd9Sstevel@tonic-gate 		if ((t_dev = get_device(NULL, dev)) == NULL) {
5107c478bd9Sstevel@tonic-gate 			continue;
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 		total_devices_found++;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		is_writer = !(check_device(t_dev, CHECK_DEVICE_NOT_WRITABLE));
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		if (is_writer) {
5177c478bd9Sstevel@tonic-gate 			writers_found++;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 			if ((writers_found == 1) && (mode != SCAN_LISTDEVS)) {
5207c478bd9Sstevel@tonic-gate 				*found = t_dev;
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		} else if ((mode == SCAN_ALL_CDS) && (writers_found == 0) &&
5247c478bd9Sstevel@tonic-gate 		    (total_devices_found == 1) && found) {
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 			/* We found a CD-ROM or DVD-ROM */
5277c478bd9Sstevel@tonic-gate 			*found = t_dev;
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 		if ((mode == SCAN_LISTDEVS) && (!defer)) {
5317c478bd9Sstevel@tonic-gate 			char *sn;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 			sn = volmgt_symname(sdev);
5347c478bd9Sstevel@tonic-gate 			if (!header_printed) {
5357c478bd9Sstevel@tonic-gate 				print_header();
5367c478bd9Sstevel@tonic-gate 				header_printed = 1;
5377c478bd9Sstevel@tonic-gate 			}
5387c478bd9Sstevel@tonic-gate 			/* show vendor, model, firmware rev and device type */
5397c478bd9Sstevel@tonic-gate 			(void) printf(" %-21.21s| %.8s %.16s %.4s | %s%s\n",
5407c478bd9Sstevel@tonic-gate 			    sn ? sn : sdev, &t_dev->d_inq[8],
5417c478bd9Sstevel@tonic-gate 			    &t_dev->d_inq[16], &t_dev->d_inq[32],
5427c478bd9Sstevel@tonic-gate 			    gettext("CD Reader"),
5437c478bd9Sstevel@tonic-gate 			    is_writer ? gettext("/Writer") : "");
5447c478bd9Sstevel@tonic-gate 			if (sn)
5457c478bd9Sstevel@tonic-gate 				free(sn);
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		if ((found != NULL) && ((*found) != t_dev))
5487c478bd9Sstevel@tonic-gate 			fini_device(t_dev);
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	(void) closedir(dir);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * If volume manager is running we'll do a retry in case the
5567c478bd9Sstevel@tonic-gate 	 * user has just inserted media, This should allow vold
5577c478bd9Sstevel@tonic-gate 	 * enough time to create the device links. If we cannot
5587c478bd9Sstevel@tonic-gate 	 * find any devices, or we find any new devices we'll retry
5597c478bd9Sstevel@tonic-gate 	 * again up to MAX_RETRIES_FOR_SCANNING
5607c478bd9Sstevel@tonic-gate 	 */
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	retry++;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	if ((retry <= MAX_RETRIES_FOR_SCANNING) && vol_running) {
5657c478bd9Sstevel@tonic-gate 		if (defer || ((mode != SCAN_LISTDEVS) &&
5667c478bd9Sstevel@tonic-gate 		    (writers_found == 0))) {
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 			if ((total_devices_found == 0) ||
5697c478bd9Sstevel@tonic-gate 			    (total_devices_found !=
5707c478bd9Sstevel@tonic-gate 			    total_devices_found_last_time)) {
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 				/* before we rescan remove the device found */
5737c478bd9Sstevel@tonic-gate 				if (total_devices_found != 0 && t_dev) {
5747c478bd9Sstevel@tonic-gate 					fini_device(t_dev);
5757c478bd9Sstevel@tonic-gate 				}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 				total_devices_found_last_time =
5787c478bd9Sstevel@tonic-gate 				    total_devices_found;
5797c478bd9Sstevel@tonic-gate 					(void) sleep(2);
5807c478bd9Sstevel@tonic-gate 					goto try_again;
5817c478bd9Sstevel@tonic-gate 			} else {
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 				/* Do the printing this time */
5847c478bd9Sstevel@tonic-gate 				defer = 0;
5857c478bd9Sstevel@tonic-gate 				goto try_again;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 			}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	if ((mode & SCAN_WRITERS) || writers_found)
5927c478bd9Sstevel@tonic-gate 		return (writers_found);
5937c478bd9Sstevel@tonic-gate 	else
5947c478bd9Sstevel@tonic-gate 		return (total_devices_found);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate  * Check device for various conditions/capabilities
5997c478bd9Sstevel@tonic-gate  * If EXIT_IF_CHECK_FAILED set in cond then it will also exit after
6007c478bd9Sstevel@tonic-gate  * printing a message.
6017c478bd9Sstevel@tonic-gate  */
6027c478bd9Sstevel@tonic-gate int
6037c478bd9Sstevel@tonic-gate check_device(cd_device *dev, int cond)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	uchar_t *disc_info, disc_status = 0, erasable = 0;
6067c478bd9Sstevel@tonic-gate 	uchar_t page_code[4];
6077c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_TYPE_NOT_CDROM) &&
6107c478bd9Sstevel@tonic-gate 	    ((dev->d_inq[0] & 0x1f) != 5)) {
6117c478bd9Sstevel@tonic-gate 		errmsg =
6127c478bd9Sstevel@tonic-gate 		    gettext("Specified device does not appear to be a CDROM");
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_READY) &&
6167c478bd9Sstevel@tonic-gate 	    !test_unit_ready(dev->d_fd)) {
6177c478bd9Sstevel@tonic-gate 		errmsg = gettext("Device not ready");
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/* Look at the capabilities page for this information */
6217c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_WRITABLE)) {
6227c478bd9Sstevel@tonic-gate 		if (!get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) ||
6237c478bd9Sstevel@tonic-gate 		    ((page_code[3] & 1) == 0)) {
6247c478bd9Sstevel@tonic-gate 			errmsg = gettext("Target device is not a CD writer");
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_NO_MEDIA)) {
6297c478bd9Sstevel@tonic-gate 		if (!test_unit_ready(dev->d_fd) && (uscsi_status == 2) &&
6307c478bd9Sstevel@tonic-gate 		    ((RQBUFLEN - rqresid) >= 14) &&
6317c478bd9Sstevel@tonic-gate 		    ((SENSE_KEY(rqbuf) & 0x0f) == 2) && (ASC(rqbuf) == 0x3A) &&
6327c478bd9Sstevel@tonic-gate 		    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
6337c478bd9Sstevel@tonic-gate 		    (ASCQ(rqbuf) == 2))) {
6347c478bd9Sstevel@tonic-gate 			/* medium not present */
6357c478bd9Sstevel@tonic-gate 			errmsg = gettext("No media in device");
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/* Issue READ DISC INFORMATION mmc command */
6427c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && ((cond & CHECK_MEDIA_IS_NOT_BLANK) ||
6437c478bd9Sstevel@tonic-gate 	    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) ||
6447c478bd9Sstevel@tonic-gate 	    (cond & CHECK_MEDIA_IS_NOT_ERASABLE))) {
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		disc_info = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
6477c478bd9Sstevel@tonic-gate 		if (!read_disc_info(dev->d_fd, disc_info)) {
6487c478bd9Sstevel@tonic-gate 			errmsg = gettext("Cannot obtain disc information");
6497c478bd9Sstevel@tonic-gate 		} else {
6507c478bd9Sstevel@tonic-gate 			disc_status = disc_info[2] & 0x03;
6517c478bd9Sstevel@tonic-gate 			erasable = disc_info[2] & 0x10;
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 		free(disc_info);
6547c478bd9Sstevel@tonic-gate 		if (errmsg == NULL) {
6557c478bd9Sstevel@tonic-gate 			if (!erasable && (cond & CHECK_MEDIA_IS_NOT_ERASABLE))
6567c478bd9Sstevel@tonic-gate 				errmsg = gettext(
6577c478bd9Sstevel@tonic-gate 				    "Media in the device is not erasable");
6587c478bd9Sstevel@tonic-gate 			else if ((disc_status != 0) &&
6597c478bd9Sstevel@tonic-gate 			    (cond & CHECK_MEDIA_IS_NOT_BLANK))
6607c478bd9Sstevel@tonic-gate 				errmsg = gettext(
6617c478bd9Sstevel@tonic-gate 				    "Media in the device is not blank");
6627c478bd9Sstevel@tonic-gate 			else if ((disc_status == 2) &&
6637c478bd9Sstevel@tonic-gate 			    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) &&
6647c478bd9Sstevel@tonic-gate 			    ((device_type != DVD_PLUS_W) &&
6657c478bd9Sstevel@tonic-gate 			    (device_type != DVD_PLUS)))
6667c478bd9Sstevel@tonic-gate 				errmsg = gettext(
6677c478bd9Sstevel@tonic-gate 				    "Media in the device is not writable");
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (errmsg) {
6727c478bd9Sstevel@tonic-gate 		if (cond & EXIT_IF_CHECK_FAILED) {
6737c478bd9Sstevel@tonic-gate 			err_msg("%s.\n", errmsg);
6747c478bd9Sstevel@tonic-gate 			exit(1);
6757c478bd9Sstevel@tonic-gate 		}
6767c478bd9Sstevel@tonic-gate 		return (1);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 	return (0);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate  * Generic routine for writing whatever the next track is and taking
6837c478bd9Sstevel@tonic-gate  * care of the progress bar. Mode tells the track type (audio or data).
6847c478bd9Sstevel@tonic-gate  * Data from track is taken from the byte stream h
6857c478bd9Sstevel@tonic-gate  */
6867c478bd9Sstevel@tonic-gate void
6877c478bd9Sstevel@tonic-gate write_next_track(int mode, bstreamhandle h)
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate 	struct track_info *ti;
6907c478bd9Sstevel@tonic-gate 	struct trackio_error *te;
6917c478bd9Sstevel@tonic-gate 	off_t size;
6927c478bd9Sstevel@tonic-gate 
6936e168402Sarutz 	ti = (struct track_info *)my_zalloc(sizeof (*ti));
6947c478bd9Sstevel@tonic-gate 	if ((build_track_info(target, -1, ti) == 0) ||
6957c478bd9Sstevel@tonic-gate 	    ((ti->ti_flags & TI_NWA_VALID) == 0)) {
6967c478bd9Sstevel@tonic-gate 		if ((device_type == DVD_PLUS) || (device_type ==
6977c478bd9Sstevel@tonic-gate 		    DVD_PLUS_W)) {
6987c478bd9Sstevel@tonic-gate 			ti->ti_flags |= TI_NWA_VALID;
6997c478bd9Sstevel@tonic-gate 		} else {
7007c478bd9Sstevel@tonic-gate 			err_msg(gettext(
7017c478bd9Sstevel@tonic-gate 			    "Cannot get writable address for the media.\n"));
7027c478bd9Sstevel@tonic-gate 			exit(1);
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 	if (ti->ti_nwa != ti->ti_start_address) {
7067c478bd9Sstevel@tonic-gate 		err_msg(gettext(
7077c478bd9Sstevel@tonic-gate 		    "Media state is not suitable for this write mode.\n"));
7087c478bd9Sstevel@tonic-gate 		exit(1);
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 	if (mode == TRACK_MODE_DATA) {
7117c478bd9Sstevel@tonic-gate 		if (!(ti->ti_track_mode & 4)) {
7127c478bd9Sstevel@tonic-gate 			/* Write track depends upon this bit */
7137c478bd9Sstevel@tonic-gate 			ti->ti_track_mode |= TRACK_MODE_DATA;
7147c478bd9Sstevel@tonic-gate 		}
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 	size = 0;
7177c478bd9Sstevel@tonic-gate 	h->bstr_size(h, &size);
7187c478bd9Sstevel@tonic-gate 	h->bstr_rewind(h);
7197c478bd9Sstevel@tonic-gate 	te = (struct trackio_error *)my_zalloc(sizeof (*te));
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Writing track %d..."), (int)ti->ti_track_no);
7227c478bd9Sstevel@tonic-gate 	init_progress();
7230c83a891Snakanon 	if (!write_track(target, ti, h, progress, size, te)) {
7247c478bd9Sstevel@tonic-gate 		if (te->err_type == TRACKIO_ERR_USER_ABORT) {
7257c478bd9Sstevel@tonic-gate 			(void) str_print(gettext("Aborted.\n"), progress_pos);
7267c478bd9Sstevel@tonic-gate 		} else {
7277c478bd9Sstevel@tonic-gate 			if (device_type != DVD_PLUS_W) {
7287c478bd9Sstevel@tonic-gate 			/* l10n_NOTE : 'failed' as in Writing Track...failed  */
7297c478bd9Sstevel@tonic-gate 				(void) str_print(gettext("failed.\n"),
7307c478bd9Sstevel@tonic-gate 				    progress_pos);
7317c478bd9Sstevel@tonic-gate 			}
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Writing track 1...done"  */
7357c478bd9Sstevel@tonic-gate 	(void) str_print(gettext("done.\n"), progress_pos);
7367c478bd9Sstevel@tonic-gate 	free(ti);
7377c478bd9Sstevel@tonic-gate 	free(te);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate void
7417c478bd9Sstevel@tonic-gate list(void)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 	if (scan_for_cd_device(SCAN_LISTDEVS, NULL) == 0) {
7447c478bd9Sstevel@tonic-gate 		if (vol_running) {
7457c478bd9Sstevel@tonic-gate 			err_msg(gettext(
7467c478bd9Sstevel@tonic-gate 			    "No CD writers found or no media in the drive.\n"));
7477c478bd9Sstevel@tonic-gate 		} else {
7487c478bd9Sstevel@tonic-gate 			if (cur_uid != 0) {
7497c478bd9Sstevel@tonic-gate 				err_msg(gettext(
7507c478bd9Sstevel@tonic-gate 				    "Volume manager is not running.\n"));
7517c478bd9Sstevel@tonic-gate 				err_msg(gettext(
7527c478bd9Sstevel@tonic-gate "Please start volume manager or run cdrw as root to access all devices.\n"));
7537c478bd9Sstevel@tonic-gate 			} else {
7547c478bd9Sstevel@tonic-gate 				err_msg(gettext("No CD writers found.\n"));
7557c478bd9Sstevel@tonic-gate 			}
7567c478bd9Sstevel@tonic-gate 		}
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 	exit(0);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate void
7627c478bd9Sstevel@tonic-gate get_media_type(int fd)
7637c478bd9Sstevel@tonic-gate {
764c9be8e69Sec158148 	uchar_t *cap = (uchar_t *)my_zalloc(MMC_FTR_HDR_LEN);
7657c478bd9Sstevel@tonic-gate 
766c9be8e69Sec158148 	if (get_configuration(fd, MMC_FTR_PRFL_LIST,
767c9be8e69Sec158148 	    MMC_FTR_HDR_LEN, cap)) {
768c9be8e69Sec158148 		if (debug)
769c9be8e69Sec158148 			(void) print_profile_list(fd);
770c9be8e69Sec158148 		switch (read_scsi16(&cap[6])) {
7717c478bd9Sstevel@tonic-gate 			case 0x8: /* CD-ROM */
7727c478bd9Sstevel@tonic-gate 				if (debug)
7737c478bd9Sstevel@tonic-gate 					(void) printf("CD-ROM found\n");
7747c478bd9Sstevel@tonic-gate 				/*
7757c478bd9Sstevel@tonic-gate 				 * To avoid regression issues, treat as
7767c478bd9Sstevel@tonic-gate 				 * A cdrw, we will check the writable
7777c478bd9Sstevel@tonic-gate 				 * mode page to see if the media is
7787c478bd9Sstevel@tonic-gate 				 * actually writable.
7797c478bd9Sstevel@tonic-gate 				 */
7807c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
7817c478bd9Sstevel@tonic-gate 				break;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 			case 0x9: /* CD-R */
7847c478bd9Sstevel@tonic-gate 				if (debug)
7857c478bd9Sstevel@tonic-gate 					(void) printf("CD-R found\n");
7867c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
7877c478bd9Sstevel@tonic-gate 				break;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 			case 0x10: /* DVD-ROM */
7907c478bd9Sstevel@tonic-gate 				/*
7917c478bd9Sstevel@tonic-gate 				 * Have seen drives return DVD+RW media
7927c478bd9Sstevel@tonic-gate 				 * DVD-ROM, so try treating it as a DVD+RW
7937c478bd9Sstevel@tonic-gate 				 * profile. checking for writable media
7947c478bd9Sstevel@tonic-gate 				 * is done through mode page 5.
7957c478bd9Sstevel@tonic-gate 				 */
7967c478bd9Sstevel@tonic-gate 				if (debug)
7977c478bd9Sstevel@tonic-gate 					(void) printf("DVD-ROM found\n");
7987c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS_W;
7997c478bd9Sstevel@tonic-gate 				break;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 			case 0xA: /* CD-RW */
8027c478bd9Sstevel@tonic-gate 				if (debug)
8037c478bd9Sstevel@tonic-gate 					(void) printf("CD-RW found\n");
8047c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8057c478bd9Sstevel@tonic-gate 				break;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 			case 0x11: /* DVD-R */
8087c478bd9Sstevel@tonic-gate 				if (debug)
8097c478bd9Sstevel@tonic-gate 					(void) printf("DVD-R found\n");
8107c478bd9Sstevel@tonic-gate 				device_type = DVD_MINUS;
8117c478bd9Sstevel@tonic-gate 				break;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 			case 0x12: /* DVD-RAM */
8147c478bd9Sstevel@tonic-gate 				if (debug)
8157c478bd9Sstevel@tonic-gate 					(void) printf("DVD-RAM found\n");
8167c478bd9Sstevel@tonic-gate 				/* treat as CD-RW, may be a legacy drive */
8177c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8187c478bd9Sstevel@tonic-gate 				break;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 			case 0x13: /* DVD-RW restricted overwrite */
8217c478bd9Sstevel@tonic-gate 			case 0x14: /* DVD-RW sequencial */
8227c478bd9Sstevel@tonic-gate 				if (debug)
8237c478bd9Sstevel@tonic-gate 					(void) printf("DVD-RW found\n");
8247c478bd9Sstevel@tonic-gate 				device_type = DVD_MINUS;
8257c478bd9Sstevel@tonic-gate 				break;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 			case 0x1A: /* DVD+RW */
8287c478bd9Sstevel@tonic-gate 				if (debug)
8297c478bd9Sstevel@tonic-gate 					(void) printf("DVD+RW found\n");
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS_W;
8327c478bd9Sstevel@tonic-gate 				break;
8337c478bd9Sstevel@tonic-gate 			case 0x1B: /* DVD+R */
8347c478bd9Sstevel@tonic-gate 				if (debug)
8357c478bd9Sstevel@tonic-gate 					(void) printf("DVD+R found\n");
8367c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS;
8377c478bd9Sstevel@tonic-gate 				break;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 			default:
8407c478bd9Sstevel@tonic-gate 				if (debug)
8417c478bd9Sstevel@tonic-gate 					(void) printf(
8427c478bd9Sstevel@tonic-gate 					"unknown drive found\n type = 0x%x",
8437c478bd9Sstevel@tonic-gate 					cap[7]);
8447c478bd9Sstevel@tonic-gate 				/*
8457c478bd9Sstevel@tonic-gate 				 * Treat as CD_RW to avoid regression, may
8467c478bd9Sstevel@tonic-gate 				 * be a legacy drive.
8477c478bd9Sstevel@tonic-gate 				 */
8487c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8497c478bd9Sstevel@tonic-gate 		}
8507c478bd9Sstevel@tonic-gate 	}
851c9be8e69Sec158148 	free(cap);
8527c478bd9Sstevel@tonic-gate }
85398584592Sarutz 
85498584592Sarutz /* Translate a transfer rate (eg, KB/s) into a Speed (eg, "2X") */
85598584592Sarutz uint_t
85698584592Sarutz cdrw_bandwidth_to_x(uint_t rate)
85798584592Sarutz {
85898584592Sarutz 	switch (device_type) {
85998584592Sarutz 	case DVD_PLUS_W:
86098584592Sarutz 	case DVD_MINUS:
86198584592Sarutz 	case DVD_PLUS:
86298584592Sarutz 		return (DVD_RATE_TO_X(rate));
86398584592Sarutz 
86498584592Sarutz 	default:
86598584592Sarutz 	case CD_RW:
86698584592Sarutz 		return (CD_RATE_TO_X(rate));
86798584592Sarutz 	}
86898584592Sarutz }
86998584592Sarutz 
87098584592Sarutz /* Translate a Speed (eg, "2X") into a transfer rate (eg, KB/s) */
87198584592Sarutz uint_t
87298584592Sarutz cdrw_x_to_bandwidth(uint_t x)
87398584592Sarutz {
87498584592Sarutz 	switch (device_type) {
87598584592Sarutz 	case DVD_PLUS_W:
87698584592Sarutz 	case DVD_MINUS:
87798584592Sarutz 	case DVD_PLUS:
87898584592Sarutz 		return (DVD_X_TO_RATE(x));
87998584592Sarutz 
88098584592Sarutz 	default:
88198584592Sarutz 	case CD_RW:
88298584592Sarutz 		return (CD_X_TO_RATE(x));
88398584592Sarutz 	}
88498584592Sarutz }
885