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