xref: /titanic_53/usr/src/cmd/cdrw/device.c (revision aefd6f194700c81d74701a1de0b4bcb83c828694)
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
561ee869eSrameshc  * Common Development and Distribution License (the "License").
661ee869eSrameshc  * 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 /*
22b0e06311Szk194757  * Copyright 2008 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 <errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/stat.h>
327c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <dirent.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <limits.h>
39b0e06311Szk194757 #include <dbus/dbus.h>
40b0e06311Szk194757 #include <hal/libhal.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 
1844becbc5bSrameshc 	/*
1854becbc5bSrameshc 	 * a workaround for the firmware problem in LITE-ON COMBO drives.
1864becbc5bSrameshc 	 * streaming for these drives sets it only to max speed regardless
1874becbc5bSrameshc 	 * of requested speed. cd_speed_ctrl allow speeds less than max
1884becbc5bSrameshc 	 * to be set but not max thus the code below. (x48 is max speed
1894becbc5bSrameshc 	 * for these drives).
1904becbc5bSrameshc 	 */
1914becbc5bSrameshc 	if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
1924becbc5bSrameshc 	    (strncmp("COMBO SOHC-4836VS",
1934becbc5bSrameshc 	    (const char *)&dev->d_inq[16], 17) == 0))
1944becbc5bSrameshc 		if (requested_speed < 48)
1954becbc5bSrameshc 			use_cd_speed = 1;
1964becbc5bSrameshc 
1977c478bd9Sstevel@tonic-gate 	cap = (uchar_t *)my_zalloc(8);
1987c478bd9Sstevel@tonic-gate 	if (is_old_sun_drive(dev)) {
1997c478bd9Sstevel@tonic-gate 		dev->d_read_audio = toshiba_read_audio;
2007c478bd9Sstevel@tonic-gate 		dev->d_speed_ctrl = toshiba_speed_ctrl;
2017c478bd9Sstevel@tonic-gate 	} else {
2027c478bd9Sstevel@tonic-gate 		/*
203c9be8e69Sec158148 		 * If the CD Read Feature is supported, READ CD will work
204c9be8e69Sec158148 		 * and will return jitter free audio data. Otherwise, look
205c9be8e69Sec158148 		 * at Page Code 2A for this information.
2067c478bd9Sstevel@tonic-gate 		 */
207c9be8e69Sec158148 		if (ftr_supported(fd, MMC_FTR_CD_READ) == 1) {
2087c478bd9Sstevel@tonic-gate 			dev->d_read_audio = read_audio_through_read_cd;
2097c478bd9Sstevel@tonic-gate 			dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
2107c478bd9Sstevel@tonic-gate 		} else if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
2117c478bd9Sstevel@tonic-gate 			if (cap[5] & 1) {
2127c478bd9Sstevel@tonic-gate 				dev->d_read_audio = read_audio_through_read_cd;
2137c478bd9Sstevel@tonic-gate 				if (cap[5] & 2)
2147c478bd9Sstevel@tonic-gate 					dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
2157c478bd9Sstevel@tonic-gate 			}
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 		/*
218c9be8e69Sec158148 		 * If the Real Time Streaming Feature is supported then
21961ee869eSrameshc 		 * Real-time streaming commands can be used for speed control
22061ee869eSrameshc 		 * (except when we want to use cd_speed_ctrl explicitly which
22161ee869eSrameshc 		 * is specified by setting use_cd_speed to 1).
222c9be8e69Sec158148 		 * Otherwise try SET CD SPEED.
2237c478bd9Sstevel@tonic-gate 		 */
22461ee869eSrameshc 		if ((ftr_supported(fd, MMC_FTR_RT_STREAM) == 1) &&
22561ee869eSrameshc 		    !use_cd_speed) {
2267c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = rt_streaming_ctrl;
2277c478bd9Sstevel@tonic-gate 			if (debug)
2287c478bd9Sstevel@tonic-gate 				(void) printf("using rt speed ctrl\n");
2297c478bd9Sstevel@tonic-gate 		} else {
2307c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = cd_speed_ctrl;
2317c478bd9Sstevel@tonic-gate 			if (debug)
2327c478bd9Sstevel@tonic-gate 				(void) printf("using cd speed ctrl\n");
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 	if (dev->d_read_audio != NULL)
2367c478bd9Sstevel@tonic-gate 		dev->d_cap |= DEV_CAP_EXTRACT_CDDA;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	dev->d_blksize = 0;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * Find the block size of the device so we can translate
2427c478bd9Sstevel@tonic-gate 	 * the reads/writes to the device blocksize.
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGMEDIAINFO, &mediainfo) < 0) {
2467c478bd9Sstevel@tonic-gate 		/*
2477c478bd9Sstevel@tonic-gate 		 * If DKIOCGMEDIAINFO fails we'll try to get
2487c478bd9Sstevel@tonic-gate 		 * the blocksize from the device itself.
2497c478bd9Sstevel@tonic-gate 		 */
2507c478bd9Sstevel@tonic-gate 		if (debug)
2517c478bd9Sstevel@tonic-gate 			(void) printf("DKIOCGMEDIAINFO failed\n");
2527c478bd9Sstevel@tonic-gate 		if (read_capacity(fd, cap))
2537c478bd9Sstevel@tonic-gate 			dev->d_blksize = read_scsi32(cap + 4);
2547c478bd9Sstevel@tonic-gate 	} else {
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		dev->d_blksize = mediainfo.dki_lbsize;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (debug) {
2607c478bd9Sstevel@tonic-gate 		uint_t bsize;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		(void) printf("blocksize = %d\n", dev->d_blksize);
2637c478bd9Sstevel@tonic-gate 		(void) printf("read_format_capacity = %d \n",
2647c478bd9Sstevel@tonic-gate 		    read_format_capacity(fd, &bsize));
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/*
2687c478bd9Sstevel@tonic-gate 	 * Some devices will return invalid blocksizes. ie. Toshiba
2697c478bd9Sstevel@tonic-gate 	 * drives will return 2352 when an audio CD is inserted.
2707c478bd9Sstevel@tonic-gate 	 * Older Sun drives will use 512 byte block sizes. All newer
2717c478bd9Sstevel@tonic-gate 	 * drives should have 2k blocksizes.
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	if (((dev->d_blksize != 512) && (dev->d_blksize != 2048))) {
2747c478bd9Sstevel@tonic-gate 			if (is_old_sun_drive(dev)) {
2757c478bd9Sstevel@tonic-gate 				dev->d_blksize = 512;
2767c478bd9Sstevel@tonic-gate 			} else {
2777c478bd9Sstevel@tonic-gate 				dev->d_blksize = 2048;
2787c478bd9Sstevel@tonic-gate 			}
2797c478bd9Sstevel@tonic-gate 		if (debug)
2807c478bd9Sstevel@tonic-gate 			(void) printf(" switching to %d\n", dev->d_blksize);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	free(cap);
2847c478bd9Sstevel@tonic-gate 	if (user_supplied) {
2857c478bd9Sstevel@tonic-gate 		dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
2867c478bd9Sstevel@tonic-gate 		(void) strcpy(dev->d_name, user_supplied);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("Got device %s\n", node));
2897c478bd9Sstevel@tonic-gate 	return (dev);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate void
2937c478bd9Sstevel@tonic-gate fini_device(cd_device *dev)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	free(dev->d_inq);
2967c478bd9Sstevel@tonic-gate 	free(dev->d_node);
2977c478bd9Sstevel@tonic-gate 	(void) close(dev->d_fd);
2987c478bd9Sstevel@tonic-gate 	if (dev->d_name)
2997c478bd9Sstevel@tonic-gate 		free(dev->d_name);
3007c478bd9Sstevel@tonic-gate 	free(dev);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
303b0e06311Szk194757 /*
304b0e06311Szk194757  * Given a /dev path resolve that path to a symbolic
305b0e06311Szk194757  * name such as cdrom0 if hald is running. If hald is
306b0e06311Szk194757  * not running, or does not have a symbolic name for the
307b0e06311Szk194757  * the specified /dev path return NULL.
308b0e06311Szk194757  */
309b0e06311Szk194757 static char *
310b0e06311Szk194757 hald_symname(char *path)
311b0e06311Szk194757 {
312b0e06311Szk194757 	LibHalContext *ctx = NULL;
313b0e06311Szk194757 	DBusError error;
314b0e06311Szk194757 
315b0e06311Szk194757 	char **udi, *p = NULL;
316b0e06311Szk194757 	int ndevs = 0, i;
317b0e06311Szk194757 
318b0e06311Szk194757 	/* Make sure hald is running */
319b0e06311Szk194757 	if (vol_running == 0)
320b0e06311Szk194757 		return (p);
321b0e06311Szk194757 
322b0e06311Szk194757 	dbus_error_init(&error);
323b0e06311Szk194757 
324b0e06311Szk194757 	if ((ctx = attach_to_hald()) == NULL)
325b0e06311Szk194757 		return (p);
326b0e06311Szk194757 
327b0e06311Szk194757 	if ((udi = libhal_manager_find_device_string_match(ctx,
328b0e06311Szk194757 	    HAL_RDSK_PROP, path, &ndevs, &error)) == NULL)
329b0e06311Szk194757 		goto done;
330b0e06311Szk194757 
331b0e06311Szk194757 	/* Look for the node that contains the valid (non-null) symdev */
332b0e06311Szk194757 	for (i = 0; i < ndevs; i++) {
333b0e06311Szk194757 		if ((p = libhal_device_get_property_string(ctx, udi[i],
334b0e06311Szk194757 		    HAL_SYMDEV_PROP, NULL)) != NULL)
335b0e06311Szk194757 			break;
336b0e06311Szk194757 		else
337b0e06311Szk194757 			libhal_free_string(p);
338b0e06311Szk194757 	}
339b0e06311Szk194757 
340b0e06311Szk194757 done:
341b0e06311Szk194757 	if (udi != NULL)
342b0e06311Szk194757 		libhal_free_string_array(udi);
343b0e06311Szk194757 	if (dbus_error_is_set(&error))
344b0e06311Szk194757 		dbus_error_free(&error);
345b0e06311Szk194757 	detach_from_hald(ctx, HAL_INITIALIZED);
346b0e06311Szk194757 	return (p);
347b0e06311Szk194757 }
348b0e06311Szk194757 
349b0e06311Szk194757 /*
350b0e06311Szk194757  * Given a name resolve that name to a raw device in the case
351b0e06311Szk194757  * that it is a symbolic name or just return what is given if
352b0e06311Szk194757  * we are given a /dev path or hald is not running.
353b0e06311Szk194757  */
354b0e06311Szk194757 static char *
355b0e06311Szk194757 hald_findname(char *symname)
356b0e06311Szk194757 {
357b0e06311Szk194757 	LibHalContext *ctx = NULL;
358b0e06311Szk194757 	DBusError error;
359b0e06311Szk194757 
360b0e06311Szk194757 	char **udi, *path = NULL;
361b0e06311Szk194757 	int ndevs = 0, i;
362b0e06311Szk194757 
363b0e06311Szk194757 	/* We already have a raw path just return that */
364b0e06311Szk194757 	if (symname[0] == '/')
365b0e06311Szk194757 		return (symname);
366b0e06311Szk194757 
367b0e06311Szk194757 	/* Get the raw device from the hal record */
368b0e06311Szk194757 	if (vol_running != 0) {
369b0e06311Szk194757 		dbus_error_init(&error);
370b0e06311Szk194757 
371b0e06311Szk194757 		if ((ctx = attach_to_hald()) == NULL)
372b0e06311Szk194757 			return (path);
373b0e06311Szk194757 
374b0e06311Szk194757 		if ((udi = libhal_manager_find_device_string_match(ctx,
375b0e06311Szk194757 		    HAL_SYMDEV_PROP, symname, &ndevs,
376b0e06311Szk194757 		    &error)) == NULL)
377b0e06311Szk194757 			goto done;
378b0e06311Szk194757 
379b0e06311Szk194757 		/*
380b0e06311Szk194757 		 * Loop over the returned UDIs to access the raw
381b0e06311Szk194757 		 * device path.
382b0e06311Szk194757 		 */
383b0e06311Szk194757 		for (i = 0; i < ndevs; i++) {
384b0e06311Szk194757 			if ((path = libhal_device_get_property_string(ctx,
385b0e06311Szk194757 			    udi[i], HAL_RDSK_PROP, NULL)) != NULL)
386b0e06311Szk194757 				break;
387b0e06311Szk194757 			else
388b0e06311Szk194757 				libhal_free_string(path);
389b0e06311Szk194757 		}
390b0e06311Szk194757 
391b0e06311Szk194757 done:
392b0e06311Szk194757 		if (udi != NULL)
393b0e06311Szk194757 			libhal_free_string_array(udi);
394b0e06311Szk194757 		if (dbus_error_is_set(&error))
395b0e06311Szk194757 			dbus_error_free(&error);
396b0e06311Szk194757 		detach_from_hald(ctx, HAL_INITIALIZED);
397b0e06311Szk194757 		return (path);
398b0e06311Szk194757 	} else {
399b0e06311Szk194757 		return (NULL);
400b0e06311Szk194757 	}
401b0e06311Szk194757 }
402b0e06311Szk194757 
4037c478bd9Sstevel@tonic-gate static int
4047c478bd9Sstevel@tonic-gate vol_name_to_dev_node(char *vname, char *found)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	struct stat statbuf;
4077c478bd9Sstevel@tonic-gate 	char *p1;
4087c478bd9Sstevel@tonic-gate 	int i;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (vname == NULL)
4117c478bd9Sstevel@tonic-gate 		return (0);
412b0e06311Szk194757 
413b0e06311Szk194757 	p1 = hald_findname(vname);
414b0e06311Szk194757 
4157c478bd9Sstevel@tonic-gate 	if (p1 == NULL)
4167c478bd9Sstevel@tonic-gate 		return (0);
4177c478bd9Sstevel@tonic-gate 	if (stat(p1, &statbuf) < 0) {
418b0e06311Szk194757 		libhal_free_string(p1);
4197c478bd9Sstevel@tonic-gate 		return (0);
4207c478bd9Sstevel@tonic-gate 	}
4214bc0a2efScasper 	if (S_ISDIR(statbuf.st_mode)) {
4227c478bd9Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
4237c478bd9Sstevel@tonic-gate 			(void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
4247c478bd9Sstevel@tonic-gate 			if (access(found, F_OK) >= 0)
4257c478bd9Sstevel@tonic-gate 				break;
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 		if (i == 16) {
428b0e06311Szk194757 			libhal_free_string(p1);
4297c478bd9Sstevel@tonic-gate 			return (0);
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 	} else {
4327c478bd9Sstevel@tonic-gate 		(void) strlcpy(found, p1, PATH_MAX);
4337c478bd9Sstevel@tonic-gate 	}
434b0e06311Szk194757 	libhal_free_string(p1);
4357c478bd9Sstevel@tonic-gate 	return (1);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * Builds an open()able device path from a user supplied node which can be
4407c478bd9Sstevel@tonic-gate  * of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
4417c478bd9Sstevel@tonic-gate  * cdrom[n]
4427c478bd9Sstevel@tonic-gate  * returns the path found in 'found' and returns 1. Otherwise returns 0.
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate int
4457c478bd9Sstevel@tonic-gate lookup_device(char *supplied, char *found)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	struct stat statbuf;
4487c478bd9Sstevel@tonic-gate 	int fd;
4497c478bd9Sstevel@tonic-gate 	char tmpstr[PATH_MAX];
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/* If everything is fine and proper, no need to analyze */
4524bc0a2efScasper 	if ((stat(supplied, &statbuf) == 0) && S_ISCHR(statbuf.st_mode) &&
4537c478bd9Sstevel@tonic-gate 	    ((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
4547c478bd9Sstevel@tonic-gate 		(void) close(fd);
4557c478bd9Sstevel@tonic-gate 		(void) strlcpy(found, supplied, PATH_MAX);
4567c478bd9Sstevel@tonic-gate 		return (1);
4577c478bd9Sstevel@tonic-gate 	}
458b0e06311Szk194757 
459b0e06311Szk194757 	/*
460b0e06311Szk194757 	 * Hal only allows access to a device when the user is
461b0e06311Szk194757 	 * on the console, therefore if hal is running and we can't
462b0e06311Szk194757 	 * open the /dev/rdsk or /dev/removable-media/rdsk device
463b0e06311Szk194757 	 * file we will return 0 marking this device as not avaiable.
464b0e06311Szk194757 	 */
465b0e06311Szk194757 	if (fd < 0 && ((strncmp(supplied, "/dev/rdsk/", 10) == 0) ||
466b0e06311Szk194757 	    (strncmp(supplied, "/dev/removable-media/rdsk/", 26) == 0)))
467b0e06311Szk194757 		return (0);
468b0e06311Szk194757 
469b0e06311Szk194757 	if ((strncmp(supplied, "/dev/dsk/", 9) == 0) ||
470b0e06311Szk194757 	    (strncmp(supplied, "/dev/removable-media/dsk/", 25) == 0)) {
4717c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
4727c478bd9Sstevel@tonic-gate 		    (char *)strrchr(supplied, '/'));
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
4757c478bd9Sstevel@tonic-gate 			(void) close(fd);
4767c478bd9Sstevel@tonic-gate 			(void) strlcpy(found, supplied, PATH_MAX);
4777c478bd9Sstevel@tonic-gate 			return (1);
4787c478bd9Sstevel@tonic-gate 		}
479b0e06311Szk194757 
480b0e06311Szk194757 		/* This device can't be opened mark it as unavailable. */
4817c478bd9Sstevel@tonic-gate 		return (0);
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 	if ((strncmp(supplied, "cdrom", 5) != 0) &&
4847c478bd9Sstevel@tonic-gate 	    (strlen(supplied) < 32)) {
4857c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
4867c478bd9Sstevel@tonic-gate 		    supplied);
4877c478bd9Sstevel@tonic-gate 		if (access(tmpstr, F_OK) < 0) {
4887c478bd9Sstevel@tonic-gate 			(void) strcat(tmpstr, "s2");
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
4917c478bd9Sstevel@tonic-gate 			(void) close(fd);
4927c478bd9Sstevel@tonic-gate 			(void) strlcpy(found, tmpstr, PATH_MAX);
4937c478bd9Sstevel@tonic-gate 			return (1);
4947c478bd9Sstevel@tonic-gate 		}
495b0e06311Szk194757 
496b0e06311Szk194757 		/* This device can't be opened mark it as unavailable. */
497b0e06311Szk194757 		return (0);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 	return (vol_name_to_dev_node(supplied, found));
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * Opens the device node name passed and returns 1 (true) if the
5047c478bd9Sstevel@tonic-gate  * device is a CD.
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate static int
5087c478bd9Sstevel@tonic-gate is_cd(char *node)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	int fd;
5117c478bd9Sstevel@tonic-gate 	struct dk_cinfo cinfo;
5127c478bd9Sstevel@tonic-gate 	int ret = 1;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	fd = open(node, O_RDONLY|O_NDELAY);
5157c478bd9Sstevel@tonic-gate 	if (fd < 0) {
5167c478bd9Sstevel@tonic-gate 		ret = 0;
5177c478bd9Sstevel@tonic-gate 	} else if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
5187c478bd9Sstevel@tonic-gate 		ret = 0;
5197c478bd9Sstevel@tonic-gate 	} else if (cinfo.dki_ctype != DKC_CDROM) {
5207c478bd9Sstevel@tonic-gate 		ret = 0;
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
5247c478bd9Sstevel@tonic-gate 		(void) close(fd);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	return (ret);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate static void
5307c478bd9Sstevel@tonic-gate print_header(void)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : Column spacing should be kept same */
5337c478bd9Sstevel@tonic-gate 	(void) printf(gettext("    Node	           Connected Device"));
5347c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : Column spacing should be kept same */
5357c478bd9Sstevel@tonic-gate 	(void) printf(gettext("	           Device type\n"));
5367c478bd9Sstevel@tonic-gate 	(void) printf(
5377c478bd9Sstevel@tonic-gate 	    "----------------------+--------------------------------");
5387c478bd9Sstevel@tonic-gate 	(void) printf("+-----------------\n");
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate /*
5427c478bd9Sstevel@tonic-gate  * returns the number of writers or CD/DVD-roms found and the path of
5437c478bd9Sstevel@tonic-gate  * the first device found depending on the mode argument.
5447c478bd9Sstevel@tonic-gate  * possible mode values are:
5457c478bd9Sstevel@tonic-gate  * SCAN_ALL_CDS 	Scan all CD/DVD devices. Return first CD-RW found.
5467c478bd9Sstevel@tonic-gate  * SCAN_WRITERS		Scan all CD-RW devices. Return first one found.
5477c478bd9Sstevel@tonic-gate  * SCAN_LISTDEVS	List all devices found.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate int
5507c478bd9Sstevel@tonic-gate scan_for_cd_device(int mode, cd_device **found)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	DIR *dir;
5537c478bd9Sstevel@tonic-gate 	struct dirent *dirent;
5547c478bd9Sstevel@tonic-gate 	char sdev[PATH_MAX], dev[PATH_MAX];
5557c478bd9Sstevel@tonic-gate 	cd_device *t_dev;
5567c478bd9Sstevel@tonic-gate 	int writers_found = 0;
5577c478bd9Sstevel@tonic-gate 	int header_printed = 0;
5587c478bd9Sstevel@tonic-gate 	int is_writer;
5597c478bd9Sstevel@tonic-gate 	int total_devices_found;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode));
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	if (mode) {
5647c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Looking for CD devices...\n"));
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	dir = opendir("/dev/rdsk");
5687c478bd9Sstevel@tonic-gate 	if (dir == NULL)
5697c478bd9Sstevel@tonic-gate 		return (0);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	writers_found = 0;
5727c478bd9Sstevel@tonic-gate 	total_devices_found = 0;
5737c478bd9Sstevel@tonic-gate 	while ((dirent = readdir(dir)) != NULL) {
5747c478bd9Sstevel@tonic-gate 		if (dirent->d_name[0] == '.')
5757c478bd9Sstevel@tonic-gate 			continue;
5767c478bd9Sstevel@tonic-gate 		(void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
5777c478bd9Sstevel@tonic-gate 		    dirent->d_name);
5787c478bd9Sstevel@tonic-gate 		if (strcmp("s2", (char *)strrchr(sdev, 's')) != 0)
5797c478bd9Sstevel@tonic-gate 			continue;
5807c478bd9Sstevel@tonic-gate 		if (!lookup_device(sdev, dev))
5817c478bd9Sstevel@tonic-gate 			continue;
5827c478bd9Sstevel@tonic-gate 		if (!is_cd(dev))
5837c478bd9Sstevel@tonic-gate 			continue;
5847c478bd9Sstevel@tonic-gate 		if ((t_dev = get_device(NULL, dev)) == NULL) {
5857c478bd9Sstevel@tonic-gate 			continue;
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		total_devices_found++;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		is_writer = !(check_device(t_dev, CHECK_DEVICE_NOT_WRITABLE));
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		if (is_writer) {
5927c478bd9Sstevel@tonic-gate 			writers_found++;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 			if ((writers_found == 1) && (mode != SCAN_LISTDEVS)) {
5957c478bd9Sstevel@tonic-gate 				*found = t_dev;
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		} else if ((mode == SCAN_ALL_CDS) && (writers_found == 0) &&
5997c478bd9Sstevel@tonic-gate 		    (total_devices_found == 1) && found) {
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 			/* We found a CD-ROM or DVD-ROM */
6027c478bd9Sstevel@tonic-gate 			*found = t_dev;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 
605b0e06311Szk194757 		if (mode == SCAN_LISTDEVS) {
6067c478bd9Sstevel@tonic-gate 			char *sn;
6077c478bd9Sstevel@tonic-gate 
608b0e06311Szk194757 			sn = hald_symname(sdev);
6097c478bd9Sstevel@tonic-gate 			if (!header_printed) {
6107c478bd9Sstevel@tonic-gate 				print_header();
6117c478bd9Sstevel@tonic-gate 				header_printed = 1;
6127c478bd9Sstevel@tonic-gate 			}
6137c478bd9Sstevel@tonic-gate 			/* show vendor, model, firmware rev and device type */
6147c478bd9Sstevel@tonic-gate 			(void) printf(" %-21.21s| %.8s %.16s %.4s | %s%s\n",
6157c478bd9Sstevel@tonic-gate 			    sn ? sn : sdev, &t_dev->d_inq[8],
6167c478bd9Sstevel@tonic-gate 			    &t_dev->d_inq[16], &t_dev->d_inq[32],
6177c478bd9Sstevel@tonic-gate 			    gettext("CD Reader"),
6187c478bd9Sstevel@tonic-gate 			    is_writer ? gettext("/Writer") : "");
6197c478bd9Sstevel@tonic-gate 			if (sn)
6207c478bd9Sstevel@tonic-gate 				free(sn);
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 		if ((found != NULL) && ((*found) != t_dev))
6237c478bd9Sstevel@tonic-gate 			fini_device(t_dev);
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	(void) closedir(dir);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	if ((mode & SCAN_WRITERS) || writers_found)
6297c478bd9Sstevel@tonic-gate 		return (writers_found);
6307c478bd9Sstevel@tonic-gate 	else
6317c478bd9Sstevel@tonic-gate 		return (total_devices_found);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate /*
6357c478bd9Sstevel@tonic-gate  * Check device for various conditions/capabilities
6367c478bd9Sstevel@tonic-gate  * If EXIT_IF_CHECK_FAILED set in cond then it will also exit after
6377c478bd9Sstevel@tonic-gate  * printing a message.
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@tonic-gate int
6407c478bd9Sstevel@tonic-gate check_device(cd_device *dev, int cond)
6417c478bd9Sstevel@tonic-gate {
6427c478bd9Sstevel@tonic-gate 	uchar_t *disc_info, disc_status = 0, erasable = 0;
6437c478bd9Sstevel@tonic-gate 	uchar_t page_code[4];
6447c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_TYPE_NOT_CDROM) &&
6477c478bd9Sstevel@tonic-gate 	    ((dev->d_inq[0] & 0x1f) != 5)) {
6487c478bd9Sstevel@tonic-gate 		errmsg =
6497c478bd9Sstevel@tonic-gate 		    gettext("Specified device does not appear to be a CDROM");
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_READY) &&
6537c478bd9Sstevel@tonic-gate 	    !test_unit_ready(dev->d_fd)) {
6547c478bd9Sstevel@tonic-gate 		errmsg = gettext("Device not ready");
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/* Look at the capabilities page for this information */
6587c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_WRITABLE)) {
6597c478bd9Sstevel@tonic-gate 		if (!get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) ||
6607c478bd9Sstevel@tonic-gate 		    ((page_code[3] & 1) == 0)) {
6617c478bd9Sstevel@tonic-gate 			errmsg = gettext("Target device is not a CD writer");
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_NO_MEDIA)) {
6667c478bd9Sstevel@tonic-gate 		if (!test_unit_ready(dev->d_fd) && (uscsi_status == 2) &&
6677c478bd9Sstevel@tonic-gate 		    ((RQBUFLEN - rqresid) >= 14) &&
6687c478bd9Sstevel@tonic-gate 		    ((SENSE_KEY(rqbuf) & 0x0f) == 2) && (ASC(rqbuf) == 0x3A) &&
6697c478bd9Sstevel@tonic-gate 		    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
6707c478bd9Sstevel@tonic-gate 		    (ASCQ(rqbuf) == 2))) {
6717c478bd9Sstevel@tonic-gate 			/* medium not present */
6727c478bd9Sstevel@tonic-gate 			errmsg = gettext("No media in device");
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/* Issue READ DISC INFORMATION mmc command */
6797c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && ((cond & CHECK_MEDIA_IS_NOT_BLANK) ||
6807c478bd9Sstevel@tonic-gate 	    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) ||
6817c478bd9Sstevel@tonic-gate 	    (cond & CHECK_MEDIA_IS_NOT_ERASABLE))) {
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 		disc_info = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
6847c478bd9Sstevel@tonic-gate 		if (!read_disc_info(dev->d_fd, disc_info)) {
6857c478bd9Sstevel@tonic-gate 			errmsg = gettext("Cannot obtain disc information");
6867c478bd9Sstevel@tonic-gate 		} else {
6877c478bd9Sstevel@tonic-gate 			disc_status = disc_info[2] & 0x03;
6887c478bd9Sstevel@tonic-gate 			erasable = disc_info[2] & 0x10;
6897c478bd9Sstevel@tonic-gate 		}
6907c478bd9Sstevel@tonic-gate 		free(disc_info);
6917c478bd9Sstevel@tonic-gate 		if (errmsg == NULL) {
6927c478bd9Sstevel@tonic-gate 			if (!erasable && (cond & CHECK_MEDIA_IS_NOT_ERASABLE))
6937c478bd9Sstevel@tonic-gate 				errmsg = gettext(
6947c478bd9Sstevel@tonic-gate 				    "Media in the device is not erasable");
6957c478bd9Sstevel@tonic-gate 			else if ((disc_status != 0) &&
6967c478bd9Sstevel@tonic-gate 			    (cond & CHECK_MEDIA_IS_NOT_BLANK))
6977c478bd9Sstevel@tonic-gate 				errmsg = gettext(
6987c478bd9Sstevel@tonic-gate 				    "Media in the device is not blank");
6997c478bd9Sstevel@tonic-gate 			else if ((disc_status == 2) &&
7007c478bd9Sstevel@tonic-gate 			    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) &&
7017c478bd9Sstevel@tonic-gate 			    ((device_type != DVD_PLUS_W) &&
7027c478bd9Sstevel@tonic-gate 			    (device_type != DVD_PLUS)))
7037c478bd9Sstevel@tonic-gate 				errmsg = gettext(
7047c478bd9Sstevel@tonic-gate 				    "Media in the device is not writable");
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (errmsg) {
7097c478bd9Sstevel@tonic-gate 		if (cond & EXIT_IF_CHECK_FAILED) {
7107c478bd9Sstevel@tonic-gate 			err_msg("%s.\n", errmsg);
7117c478bd9Sstevel@tonic-gate 			exit(1);
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 		return (1);
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 	return (0);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate /*
7197c478bd9Sstevel@tonic-gate  * Generic routine for writing whatever the next track is and taking
7207c478bd9Sstevel@tonic-gate  * care of the progress bar. Mode tells the track type (audio or data).
7217c478bd9Sstevel@tonic-gate  * Data from track is taken from the byte stream h
7227c478bd9Sstevel@tonic-gate  */
7237c478bd9Sstevel@tonic-gate void
7247c478bd9Sstevel@tonic-gate write_next_track(int mode, bstreamhandle h)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate 	struct track_info *ti;
7277c478bd9Sstevel@tonic-gate 	struct trackio_error *te;
7287c478bd9Sstevel@tonic-gate 	off_t size;
7297c478bd9Sstevel@tonic-gate 
7306e168402Sarutz 	ti = (struct track_info *)my_zalloc(sizeof (*ti));
7317c478bd9Sstevel@tonic-gate 	if ((build_track_info(target, -1, ti) == 0) ||
7327c478bd9Sstevel@tonic-gate 	    ((ti->ti_flags & TI_NWA_VALID) == 0)) {
7337c478bd9Sstevel@tonic-gate 		if ((device_type == DVD_PLUS) || (device_type ==
7347c478bd9Sstevel@tonic-gate 		    DVD_PLUS_W)) {
7357c478bd9Sstevel@tonic-gate 			ti->ti_flags |= TI_NWA_VALID;
7367c478bd9Sstevel@tonic-gate 		} else {
7377c478bd9Sstevel@tonic-gate 			err_msg(gettext(
7387c478bd9Sstevel@tonic-gate 			    "Cannot get writable address for the media.\n"));
7397c478bd9Sstevel@tonic-gate 			exit(1);
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	if (ti->ti_nwa != ti->ti_start_address) {
7437c478bd9Sstevel@tonic-gate 		err_msg(gettext(
7447c478bd9Sstevel@tonic-gate 		    "Media state is not suitable for this write mode.\n"));
7457c478bd9Sstevel@tonic-gate 		exit(1);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 	if (mode == TRACK_MODE_DATA) {
7487c478bd9Sstevel@tonic-gate 		if (!(ti->ti_track_mode & 4)) {
7497c478bd9Sstevel@tonic-gate 			/* Write track depends upon this bit */
7507c478bd9Sstevel@tonic-gate 			ti->ti_track_mode |= TRACK_MODE_DATA;
7517c478bd9Sstevel@tonic-gate 		}
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 	size = 0;
7547c478bd9Sstevel@tonic-gate 	h->bstr_size(h, &size);
7557c478bd9Sstevel@tonic-gate 	h->bstr_rewind(h);
7567c478bd9Sstevel@tonic-gate 	te = (struct trackio_error *)my_zalloc(sizeof (*te));
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Writing track %d..."), (int)ti->ti_track_no);
7597c478bd9Sstevel@tonic-gate 	init_progress();
7600c83a891Snakanon 	if (!write_track(target, ti, h, progress, size, te)) {
7617c478bd9Sstevel@tonic-gate 		if (te->err_type == TRACKIO_ERR_USER_ABORT) {
7627c478bd9Sstevel@tonic-gate 			(void) str_print(gettext("Aborted.\n"), progress_pos);
7637c478bd9Sstevel@tonic-gate 		} else {
7647c478bd9Sstevel@tonic-gate 			if (device_type != DVD_PLUS_W) {
7657c478bd9Sstevel@tonic-gate 			/* l10n_NOTE : 'failed' as in Writing Track...failed  */
7667c478bd9Sstevel@tonic-gate 				(void) str_print(gettext("failed.\n"),
7677c478bd9Sstevel@tonic-gate 				    progress_pos);
7687c478bd9Sstevel@tonic-gate 			}
7697c478bd9Sstevel@tonic-gate 		}
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Writing track 1...done"  */
7727c478bd9Sstevel@tonic-gate 	(void) str_print(gettext("done.\n"), progress_pos);
7737c478bd9Sstevel@tonic-gate 	free(ti);
7747c478bd9Sstevel@tonic-gate 	free(te);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate void
7787c478bd9Sstevel@tonic-gate list(void)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	if (scan_for_cd_device(SCAN_LISTDEVS, NULL) == 0) {
7817c478bd9Sstevel@tonic-gate 		if (vol_running) {
7827c478bd9Sstevel@tonic-gate 			err_msg(gettext(
783b0e06311Szk194757 			    "No CD writers found, no media in the drive "
784b0e06311Szk194757 			    "or not on the console.\n"));
7857c478bd9Sstevel@tonic-gate 		} else {
7867c478bd9Sstevel@tonic-gate 			if (cur_uid != 0) {
7877c478bd9Sstevel@tonic-gate 				err_msg(gettext(
7887c478bd9Sstevel@tonic-gate 				    "Volume manager is not running.\n"));
7897c478bd9Sstevel@tonic-gate 				err_msg(gettext(
7907c478bd9Sstevel@tonic-gate "Please start volume manager or run cdrw as root to access all devices.\n"));
7917c478bd9Sstevel@tonic-gate 			} else {
7927c478bd9Sstevel@tonic-gate 				err_msg(gettext("No CD writers found.\n"));
7937c478bd9Sstevel@tonic-gate 			}
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 	exit(0);
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate void
8007c478bd9Sstevel@tonic-gate get_media_type(int fd)
8017c478bd9Sstevel@tonic-gate {
802c9be8e69Sec158148 	uchar_t *cap = (uchar_t *)my_zalloc(MMC_FTR_HDR_LEN);
8037c478bd9Sstevel@tonic-gate 
804c9be8e69Sec158148 	if (get_configuration(fd, MMC_FTR_PRFL_LIST,
805c9be8e69Sec158148 	    MMC_FTR_HDR_LEN, cap)) {
806c9be8e69Sec158148 		if (debug)
807c9be8e69Sec158148 			(void) print_profile_list(fd);
808c9be8e69Sec158148 		switch (read_scsi16(&cap[6])) {
8097c478bd9Sstevel@tonic-gate 			case 0x8: /* CD-ROM */
8107c478bd9Sstevel@tonic-gate 				if (debug)
8117c478bd9Sstevel@tonic-gate 					(void) printf("CD-ROM found\n");
8127c478bd9Sstevel@tonic-gate 				/*
8137c478bd9Sstevel@tonic-gate 				 * To avoid regression issues, treat as
8147c478bd9Sstevel@tonic-gate 				 * A cdrw, we will check the writable
8157c478bd9Sstevel@tonic-gate 				 * mode page to see if the media is
8167c478bd9Sstevel@tonic-gate 				 * actually writable.
8177c478bd9Sstevel@tonic-gate 				 */
8187c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8197c478bd9Sstevel@tonic-gate 				break;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 			case 0x9: /* CD-R */
8227c478bd9Sstevel@tonic-gate 				if (debug)
8237c478bd9Sstevel@tonic-gate 					(void) printf("CD-R found\n");
8247c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8257c478bd9Sstevel@tonic-gate 				break;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 			case 0x10: /* DVD-ROM */
8287c478bd9Sstevel@tonic-gate 				/*
8297c478bd9Sstevel@tonic-gate 				 * Have seen drives return DVD+RW media
8307c478bd9Sstevel@tonic-gate 				 * DVD-ROM, so try treating it as a DVD+RW
8317c478bd9Sstevel@tonic-gate 				 * profile. checking for writable media
8327c478bd9Sstevel@tonic-gate 				 * is done through mode page 5.
8337c478bd9Sstevel@tonic-gate 				 */
8347c478bd9Sstevel@tonic-gate 				if (debug)
8357c478bd9Sstevel@tonic-gate 					(void) printf("DVD-ROM found\n");
8367c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS_W;
8377c478bd9Sstevel@tonic-gate 				break;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 			case 0xA: /* CD-RW */
8407c478bd9Sstevel@tonic-gate 				if (debug)
8417c478bd9Sstevel@tonic-gate 					(void) printf("CD-RW found\n");
8427c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8437c478bd9Sstevel@tonic-gate 				break;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 			case 0x11: /* DVD-R */
8467c478bd9Sstevel@tonic-gate 				if (debug)
8477c478bd9Sstevel@tonic-gate 					(void) printf("DVD-R found\n");
8487c478bd9Sstevel@tonic-gate 				device_type = DVD_MINUS;
8497c478bd9Sstevel@tonic-gate 				break;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 			case 0x12: /* DVD-RAM */
8527c478bd9Sstevel@tonic-gate 				if (debug)
8537c478bd9Sstevel@tonic-gate 					(void) printf("DVD-RAM found\n");
8547c478bd9Sstevel@tonic-gate 				/* treat as CD-RW, may be a legacy drive */
8557c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8567c478bd9Sstevel@tonic-gate 				break;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 			case 0x13: /* DVD-RW restricted overwrite */
859b0e06311Szk194757 			case 0x14: /* DVD-RW sequential */
8607c478bd9Sstevel@tonic-gate 				if (debug)
8617c478bd9Sstevel@tonic-gate 					(void) printf("DVD-RW found\n");
8627c478bd9Sstevel@tonic-gate 				device_type = DVD_MINUS;
8637c478bd9Sstevel@tonic-gate 				break;
8647c478bd9Sstevel@tonic-gate 
865*aefd6f19Szk194757 			case 0x15: /* DVD-R Dual Layer Sequential Recording */
866*aefd6f19Szk194757 			case 0x16: /* DVD-R Dual Layer Jump Recording */
867*aefd6f19Szk194757 				if (debug)
868*aefd6f19Szk194757 					(void) printf("DVD-R DL found\n");
869*aefd6f19Szk194757 				device_type = DVD_MINUS;
870*aefd6f19Szk194757 				break;
871*aefd6f19Szk194757 
872*aefd6f19Szk194757 			case 0x17: /* DVD-RW Dual Layer */
873*aefd6f19Szk194757 				if (debug)
874*aefd6f19Szk194757 					(void) printf("DVD-RW DL found\n");
875*aefd6f19Szk194757 				device_type = DVD_MINUS;
876*aefd6f19Szk194757 				break;
877*aefd6f19Szk194757 
8787c478bd9Sstevel@tonic-gate 			case 0x1A: /* DVD+RW */
8797c478bd9Sstevel@tonic-gate 				if (debug)
8807c478bd9Sstevel@tonic-gate 					(void) printf("DVD+RW found\n");
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS_W;
8837c478bd9Sstevel@tonic-gate 				break;
884*aefd6f19Szk194757 
8857c478bd9Sstevel@tonic-gate 			case 0x1B: /* DVD+R */
8867c478bd9Sstevel@tonic-gate 				if (debug)
8877c478bd9Sstevel@tonic-gate 					(void) printf("DVD+R found\n");
8887c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS;
8897c478bd9Sstevel@tonic-gate 				break;
8907c478bd9Sstevel@tonic-gate 
891*aefd6f19Szk194757 			case 0x2A: /* DVD+RW Dual Layer */
892*aefd6f19Szk194757 				if (debug)
893*aefd6f19Szk194757 					(void) printf("DVD+RW DL found\n");
894*aefd6f19Szk194757 				device_type = DVD_PLUS_W;
895*aefd6f19Szk194757 				break;
896*aefd6f19Szk194757 
897*aefd6f19Szk194757 			case 0x2B: /* DVD+R Dual Layer */
898*aefd6f19Szk194757 				if (debug)
899*aefd6f19Szk194757 					(void) printf("DVD+R DL found\n");
900*aefd6f19Szk194757 				device_type = DVD_PLUS;
901*aefd6f19Szk194757 				break;
902*aefd6f19Szk194757 
9037c478bd9Sstevel@tonic-gate 			default:
9047c478bd9Sstevel@tonic-gate 				if (debug)
9057c478bd9Sstevel@tonic-gate 					(void) printf(
9067c478bd9Sstevel@tonic-gate 					    "unknown drive found\n type = 0x%x",
9077c478bd9Sstevel@tonic-gate 					    cap[7]);
9087c478bd9Sstevel@tonic-gate 				/*
9097c478bd9Sstevel@tonic-gate 				 * Treat as CD_RW to avoid regression, may
9107c478bd9Sstevel@tonic-gate 				 * be a legacy drive.
9117c478bd9Sstevel@tonic-gate 				 */
9127c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
9137c478bd9Sstevel@tonic-gate 		}
9147c478bd9Sstevel@tonic-gate 	}
915c9be8e69Sec158148 	free(cap);
9167c478bd9Sstevel@tonic-gate }
91798584592Sarutz 
91898584592Sarutz /* Translate a transfer rate (eg, KB/s) into a Speed (eg, "2X") */
91998584592Sarutz uint_t
92098584592Sarutz cdrw_bandwidth_to_x(uint_t rate)
92198584592Sarutz {
92298584592Sarutz 	switch (device_type) {
92398584592Sarutz 	case DVD_PLUS_W:
92498584592Sarutz 	case DVD_MINUS:
92598584592Sarutz 	case DVD_PLUS:
92698584592Sarutz 		return (DVD_RATE_TO_X(rate));
92798584592Sarutz 
92898584592Sarutz 	default:
92998584592Sarutz 	case CD_RW:
93098584592Sarutz 		return (CD_RATE_TO_X(rate));
93198584592Sarutz 	}
93298584592Sarutz }
93398584592Sarutz 
93498584592Sarutz /* Translate a Speed (eg, "2X") into a transfer rate (eg, KB/s) */
93598584592Sarutz uint_t
93698584592Sarutz cdrw_x_to_bandwidth(uint_t x)
93798584592Sarutz {
93898584592Sarutz 	switch (device_type) {
93998584592Sarutz 	case DVD_PLUS_W:
94098584592Sarutz 	case DVD_MINUS:
94198584592Sarutz 	case DVD_PLUS:
94298584592Sarutz 		return (DVD_X_TO_RATE(x));
94398584592Sarutz 
94498584592Sarutz 	default:
94598584592Sarutz 	case CD_RW:
94698584592Sarutz 		return (CD_X_TO_RATE(x));
94798584592Sarutz 	}
94898584592Sarutz }
949