xref: /illumos-gate/usr/src/cmd/cdrw/device.c (revision 42e143ab)
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 /*
22*42e143abSZach Kissel  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <fcntl.h>
287c478bd9Sstevel@tonic-gate #include <errno.h>
297c478bd9Sstevel@tonic-gate #include <sys/stat.h>
307c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <dirent.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <limits.h>
37b0e06311Szk #include <dbus/dbus.h>
38b0e06311Szk #include <hal/libhal.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "transport.h"
417c478bd9Sstevel@tonic-gate #include "mmc.h"
427c478bd9Sstevel@tonic-gate #include "device.h"
437c478bd9Sstevel@tonic-gate #include "util.h"
447c478bd9Sstevel@tonic-gate #include "msgs.h"
457c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
467c478bd9Sstevel@tonic-gate #include "toshiba.h"
477c478bd9Sstevel@tonic-gate #include "main.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Old sun drives have a vendor specific mode page for setting/getting speed.
517c478bd9Sstevel@tonic-gate  * Also they use a different method for extracting audio.
527c478bd9Sstevel@tonic-gate  * We have the device inquiry strings at this time. This is used to enable
537c478bd9Sstevel@tonic-gate  * us to use older sun drives to extract audio.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate static int
is_old_sun_drive(cd_device * dev)567c478bd9Sstevel@tonic-gate is_old_sun_drive(cd_device *dev)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	/*
597c478bd9Sstevel@tonic-gate 	 * If we have a SONY CDU 561, CDU 8012, or TOSHIBA model with XMa we
607c478bd9Sstevel@tonic-gate 	 * need to handle these drives a bit differently.
617c478bd9Sstevel@tonic-gate 	 */
627c478bd9Sstevel@tonic-gate 	if (strncmp("SONY", (const char *)&dev->d_inq[8], 4) == 0) {
637c478bd9Sstevel@tonic-gate 		if (strncmp("CDU 561", (const char *)&dev->d_inq[16], 7) == 0)
647c478bd9Sstevel@tonic-gate 			return (1);
657c478bd9Sstevel@tonic-gate 		if (strncmp("CDU-8012", (const char *)&dev->d_inq[16], 8) == 0)
667c478bd9Sstevel@tonic-gate 			return (1);
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if ((strncmp("TOSHIBA", (const char *)&dev->d_inq[8], 7) == 0) &&
707c478bd9Sstevel@tonic-gate 	    (strncmp("XM", (const char *)&dev->d_inq[16], 2) == 0)) {
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 		char product_id[17];
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 		/* Changing speed is not allowed for 32X TOSHIBA drives */
757c478bd9Sstevel@tonic-gate 		if (strncmp("SUN32XCD", (const char *)&dev->d_inq[24], 8) == 0)
767c478bd9Sstevel@tonic-gate 			dev->d_cap |= DEV_CAP_SETTING_SPEED_NOT_ALLOWED;
777c478bd9Sstevel@tonic-gate 		(void) strncpy(product_id, (const char *)&dev->d_inq[16], 16);
787c478bd9Sstevel@tonic-gate 		product_id[16] = 0;
797c478bd9Sstevel@tonic-gate 		if (strstr(product_id, "SUN") != NULL)
807c478bd9Sstevel@tonic-gate 			return (1);
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	return (0);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * returns a cd_device handle for a node returned by lookup_device()
877c478bd9Sstevel@tonic-gate  * also takes the user supplied name and stores it inside the node
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate cd_device *
get_device(char * user_supplied,char * node)907c478bd9Sstevel@tonic-gate get_device(char *user_supplied, char *node)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	cd_device *dev;
937c478bd9Sstevel@tonic-gate 	int fd;
947c478bd9Sstevel@tonic-gate 	uchar_t *cap;
957c478bd9Sstevel@tonic-gate 	char devnode[PATH_MAX];
967c478bd9Sstevel@tonic-gate 	int size;
977c478bd9Sstevel@tonic-gate 	struct dk_minfo mediainfo;
987c478bd9Sstevel@tonic-gate 	int use_cd_speed = 0;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * we need to resolve any link paths to avoid fake files
1027c478bd9Sstevel@tonic-gate 	 * such as /dev/rdsk/../../export/file.
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("get_device(%s, %s)\n", user_supplied ?
1067c478bd9Sstevel@tonic-gate 	    user_supplied : "<nil>", node ? node : "<nil>"));
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	size = resolvepath(node, devnode, PATH_MAX);
1097c478bd9Sstevel@tonic-gate 	if ((size <= 0) || (size >= (PATH_MAX - 1)))
1107c478bd9Sstevel@tonic-gate 		return (NULL);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/* resolvepath may not return a null terminated string */
1137c478bd9Sstevel@tonic-gate 	devnode[size] = '\0';
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/* the device node must be in /devices/ or /vol/dev/rdsk */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if ((strncmp(devnode, "/devices/", 9) != 0) &&
1197c478bd9Sstevel@tonic-gate 	    (strncmp(devnode, "/vol/dev/rdsk", 13) != 0))
1207c478bd9Sstevel@tonic-gate 		return (NULL);
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Since we are currently running with the user euid it is
1237c478bd9Sstevel@tonic-gate 	 * safe to try to open the file without checking access.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	fd = open(devnode, O_RDONLY|O_NDELAY);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (fd < 0) {
1297c478bd9Sstevel@tonic-gate 		TRACE(traceall_msg("Cannot open %s: %s\n", node,
1307c478bd9Sstevel@tonic-gate 		    strerror(errno)));
1317c478bd9Sstevel@tonic-gate 		return (NULL);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	dev = (cd_device *)my_zalloc(sizeof (cd_device));
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	dev->d_node = (char *)my_zalloc(strlen(devnode) + 1);
1377c478bd9Sstevel@tonic-gate 	(void) strcpy(dev->d_node, devnode);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	dev->d_fd = fd;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	dev->d_inq = (uchar_t *)my_zalloc(INQUIRY_DATA_LENGTH);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (!inquiry(fd, dev->d_inq)) {
1447c478bd9Sstevel@tonic-gate 		TRACE(traceall_msg("Inquiry failed on device %s\n", node));
1457c478bd9Sstevel@tonic-gate 		if (debug) {
1467c478bd9Sstevel@tonic-gate 			(void) printf("USCSI ioctl failed %d\n",
1477c478bd9Sstevel@tonic-gate 			    uscsi_error);
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 		free(dev->d_inq);
1507c478bd9Sstevel@tonic-gate 		free(dev->d_node);
1517c478bd9Sstevel@tonic-gate 		(void) close(dev->d_fd);
1527c478bd9Sstevel@tonic-gate 		free(dev);
1537c478bd9Sstevel@tonic-gate 		return (NULL);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (debug) {
1577c478bd9Sstevel@tonic-gate 		cap = (uchar_t *)my_zalloc(18);
1587c478bd9Sstevel@tonic-gate 		(void) printf("Checking device type\n");
1597c478bd9Sstevel@tonic-gate 		if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
1607c478bd9Sstevel@tonic-gate 			if (cap[2] & 0x10)
1617c478bd9Sstevel@tonic-gate 				(void) printf("DVD-R read support\n");
1627c478bd9Sstevel@tonic-gate 			if (cap[3] & 0x10)
1637c478bd9Sstevel@tonic-gate 				(void) printf("DVD-R write support\n");
1647c478bd9Sstevel@tonic-gate 			if (cap[5] & 0x4)
1657c478bd9Sstevel@tonic-gate 				(void) printf("R-W supported\n");
1667c478bd9Sstevel@tonic-gate 			if (cap[2] & 0x20)
1677c478bd9Sstevel@tonic-gate 				(void) printf("DVD-RAM read supported\n");
1687c478bd9Sstevel@tonic-gate 			if (cap[3] & 0x20)
1697c478bd9Sstevel@tonic-gate 				(void) printf("DVD-RAM write supported\n");
1707c478bd9Sstevel@tonic-gate 		} else {
1717c478bd9Sstevel@tonic-gate 			(void) printf("Could not read mode page 2A! \n");
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 		free(cap);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/* Detect if it's a Lite-ON drive with a streaming CD problem */
1777c478bd9Sstevel@tonic-gate 	if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
1787c478bd9Sstevel@tonic-gate 	    (strncmp("LTR-48", (const char *)&dev->d_inq[16], 6) == 0)) {
1797c478bd9Sstevel@tonic-gate 		use_cd_speed = 1;
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1824becbc5bSrameshc 	/*
1834becbc5bSrameshc 	 * a workaround for the firmware problem in LITE-ON COMBO drives.
1844becbc5bSrameshc 	 * streaming for these drives sets it only to max speed regardless
1854becbc5bSrameshc 	 * of requested speed. cd_speed_ctrl allow speeds less than max
1864becbc5bSrameshc 	 * to be set but not max thus the code below. (x48 is max speed
1874becbc5bSrameshc 	 * for these drives).
1884becbc5bSrameshc 	 */
1894becbc5bSrameshc 	if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
1904becbc5bSrameshc 	    (strncmp("COMBO SOHC-4836VS",
1914becbc5bSrameshc 	    (const char *)&dev->d_inq[16], 17) == 0))
1924becbc5bSrameshc 		if (requested_speed < 48)
1934becbc5bSrameshc 			use_cd_speed = 1;
1944becbc5bSrameshc 
1957c478bd9Sstevel@tonic-gate 	cap = (uchar_t *)my_zalloc(8);
1967c478bd9Sstevel@tonic-gate 	if (is_old_sun_drive(dev)) {
1977c478bd9Sstevel@tonic-gate 		dev->d_read_audio = toshiba_read_audio;
1987c478bd9Sstevel@tonic-gate 		dev->d_speed_ctrl = toshiba_speed_ctrl;
1997c478bd9Sstevel@tonic-gate 	} else {
2007c478bd9Sstevel@tonic-gate 		/*
201c9be8e69Sec 		 * If the CD Read Feature is supported, READ CD will work
202c9be8e69Sec 		 * and will return jitter free audio data. Otherwise, look
203c9be8e69Sec 		 * at Page Code 2A for this information.
2047c478bd9Sstevel@tonic-gate 		 */
205c9be8e69Sec 		if (ftr_supported(fd, MMC_FTR_CD_READ) == 1) {
2067c478bd9Sstevel@tonic-gate 			dev->d_read_audio = read_audio_through_read_cd;
2077c478bd9Sstevel@tonic-gate 			dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
2087c478bd9Sstevel@tonic-gate 		} else if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
2097c478bd9Sstevel@tonic-gate 			if (cap[5] & 1) {
2107c478bd9Sstevel@tonic-gate 				dev->d_read_audio = read_audio_through_read_cd;
2117c478bd9Sstevel@tonic-gate 				if (cap[5] & 2)
2127c478bd9Sstevel@tonic-gate 					dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
2137c478bd9Sstevel@tonic-gate 			}
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		/*
216c9be8e69Sec 		 * If the Real Time Streaming Feature is supported then
21761ee869eSrameshc 		 * Real-time streaming commands can be used for speed control
21861ee869eSrameshc 		 * (except when we want to use cd_speed_ctrl explicitly which
21961ee869eSrameshc 		 * is specified by setting use_cd_speed to 1).
220c9be8e69Sec 		 * Otherwise try SET CD SPEED.
2217c478bd9Sstevel@tonic-gate 		 */
22261ee869eSrameshc 		if ((ftr_supported(fd, MMC_FTR_RT_STREAM) == 1) &&
22361ee869eSrameshc 		    !use_cd_speed) {
2247c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = rt_streaming_ctrl;
2257c478bd9Sstevel@tonic-gate 			if (debug)
2267c478bd9Sstevel@tonic-gate 				(void) printf("using rt speed ctrl\n");
2277c478bd9Sstevel@tonic-gate 		} else {
2287c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = cd_speed_ctrl;
2297c478bd9Sstevel@tonic-gate 			if (debug)
2307c478bd9Sstevel@tonic-gate 				(void) printf("using cd speed ctrl\n");
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	if (dev->d_read_audio != NULL)
2347c478bd9Sstevel@tonic-gate 		dev->d_cap |= DEV_CAP_EXTRACT_CDDA;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	dev->d_blksize = 0;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/*
2397c478bd9Sstevel@tonic-gate 	 * Find the block size of the device so we can translate
2407c478bd9Sstevel@tonic-gate 	 * the reads/writes to the device blocksize.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGMEDIAINFO, &mediainfo) < 0) {
2447c478bd9Sstevel@tonic-gate 		/*
2457c478bd9Sstevel@tonic-gate 		 * If DKIOCGMEDIAINFO fails we'll try to get
2467c478bd9Sstevel@tonic-gate 		 * the blocksize from the device itself.
2477c478bd9Sstevel@tonic-gate 		 */
2487c478bd9Sstevel@tonic-gate 		if (debug)
2497c478bd9Sstevel@tonic-gate 			(void) printf("DKIOCGMEDIAINFO failed\n");
2507c478bd9Sstevel@tonic-gate 		if (read_capacity(fd, cap))
2517c478bd9Sstevel@tonic-gate 			dev->d_blksize = read_scsi32(cap + 4);
2527c478bd9Sstevel@tonic-gate 	} else {
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		dev->d_blksize = mediainfo.dki_lbsize;
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (debug) {
2587c478bd9Sstevel@tonic-gate 		uint_t bsize;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		(void) printf("blocksize = %d\n", dev->d_blksize);
2617c478bd9Sstevel@tonic-gate 		(void) printf("read_format_capacity = %d \n",
2627c478bd9Sstevel@tonic-gate 		    read_format_capacity(fd, &bsize));
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * Some devices will return invalid blocksizes. ie. Toshiba
2677c478bd9Sstevel@tonic-gate 	 * drives will return 2352 when an audio CD is inserted.
2687c478bd9Sstevel@tonic-gate 	 * Older Sun drives will use 512 byte block sizes. All newer
2697c478bd9Sstevel@tonic-gate 	 * drives should have 2k blocksizes.
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 	if (((dev->d_blksize != 512) && (dev->d_blksize != 2048))) {
2727c478bd9Sstevel@tonic-gate 			if (is_old_sun_drive(dev)) {
2737c478bd9Sstevel@tonic-gate 				dev->d_blksize = 512;
2747c478bd9Sstevel@tonic-gate 			} else {
2757c478bd9Sstevel@tonic-gate 				dev->d_blksize = 2048;
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 		if (debug)
2787c478bd9Sstevel@tonic-gate 			(void) printf(" switching to %d\n", dev->d_blksize);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	free(cap);
2827c478bd9Sstevel@tonic-gate 	if (user_supplied) {
2837c478bd9Sstevel@tonic-gate 		dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
2847c478bd9Sstevel@tonic-gate 		(void) strcpy(dev->d_name, user_supplied);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("Got device %s\n", node));
2877c478bd9Sstevel@tonic-gate 	return (dev);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate void
fini_device(cd_device * dev)2917c478bd9Sstevel@tonic-gate fini_device(cd_device *dev)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	free(dev->d_inq);
2947c478bd9Sstevel@tonic-gate 	free(dev->d_node);
2957c478bd9Sstevel@tonic-gate 	(void) close(dev->d_fd);
2967c478bd9Sstevel@tonic-gate 	if (dev->d_name)
2977c478bd9Sstevel@tonic-gate 		free(dev->d_name);
2987c478bd9Sstevel@tonic-gate 	free(dev);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
301b0e06311Szk /*
302b0e06311Szk  * Given a /dev path resolve that path to a symbolic
303b0e06311Szk  * name such as cdrom0 if hald is running. If hald is
304b0e06311Szk  * not running, or does not have a symbolic name for the
305b0e06311Szk  * the specified /dev path return NULL.
306b0e06311Szk  */
307b0e06311Szk static char *
hald_symname(char * path)308b0e06311Szk hald_symname(char *path)
309b0e06311Szk {
310b0e06311Szk 	LibHalContext *ctx = NULL;
311b0e06311Szk 	DBusError error;
312b0e06311Szk 
313b0e06311Szk 	char **udi, *p = NULL;
314b0e06311Szk 	int ndevs = 0, i;
315b0e06311Szk 
316b0e06311Szk 	/* Make sure hald is running */
317b0e06311Szk 	if (vol_running == 0)
318b0e06311Szk 		return (p);
319b0e06311Szk 
320b0e06311Szk 	dbus_error_init(&error);
321b0e06311Szk 
322b0e06311Szk 	if ((ctx = attach_to_hald()) == NULL)
323b0e06311Szk 		return (p);
324b0e06311Szk 
325b0e06311Szk 	if ((udi = libhal_manager_find_device_string_match(ctx,
326b0e06311Szk 	    HAL_RDSK_PROP, path, &ndevs, &error)) == NULL)
327b0e06311Szk 		goto done;
328b0e06311Szk 
329b0e06311Szk 	/* Look for the node that contains the valid (non-null) symdev */
330b0e06311Szk 	for (i = 0; i < ndevs; i++) {
331b0e06311Szk 		if ((p = libhal_device_get_property_string(ctx, udi[i],
332b0e06311Szk 		    HAL_SYMDEV_PROP, NULL)) != NULL)
333b0e06311Szk 			break;
334b0e06311Szk 		else
335b0e06311Szk 			libhal_free_string(p);
336b0e06311Szk 	}
337b0e06311Szk 
338b0e06311Szk done:
339b0e06311Szk 	if (udi != NULL)
340b0e06311Szk 		libhal_free_string_array(udi);
341b0e06311Szk 	if (dbus_error_is_set(&error))
342b0e06311Szk 		dbus_error_free(&error);
343b0e06311Szk 	detach_from_hald(ctx, HAL_INITIALIZED);
344b0e06311Szk 	return (p);
345b0e06311Szk }
346b0e06311Szk 
347b0e06311Szk /*
348b0e06311Szk  * Given a name resolve that name to a raw device in the case
349b0e06311Szk  * that it is a symbolic name or just return what is given if
350b0e06311Szk  * we are given a /dev path or hald is not running.
351b0e06311Szk  */
352b0e06311Szk static char *
hald_findname(char * symname)353b0e06311Szk hald_findname(char *symname)
354b0e06311Szk {
355b0e06311Szk 	LibHalContext *ctx = NULL;
356b0e06311Szk 	DBusError error;
357b0e06311Szk 
358b0e06311Szk 	char **udi, *path = NULL;
359b0e06311Szk 	int ndevs = 0, i;
360b0e06311Szk 
361*42e143abSZach Kissel 	/*
362*42e143abSZach Kissel 	 * We already have a raw path lets return it in a copied buffer
363*42e143abSZach Kissel 	 * as our caller assumes that they need to free memory.
364*42e143abSZach Kissel 	 */
365*42e143abSZach Kissel 	if (symname[0] == '/') {
366*42e143abSZach Kissel 		path = my_zalloc(strlen(symname) + 1);
367*42e143abSZach Kissel 		(void) strlcpy(path, symname, (strlen(symname) + 1));
368*42e143abSZach Kissel 		return (path);
369*42e143abSZach Kissel 	}
370b0e06311Szk 
371b0e06311Szk 	/* Get the raw device from the hal record */
372b0e06311Szk 	if (vol_running != 0) {
373b0e06311Szk 		dbus_error_init(&error);
374b0e06311Szk 
375b0e06311Szk 		if ((ctx = attach_to_hald()) == NULL)
376b0e06311Szk 			return (path);
377b0e06311Szk 
378b0e06311Szk 		if ((udi = libhal_manager_find_device_string_match(ctx,
379b0e06311Szk 		    HAL_SYMDEV_PROP, symname, &ndevs,
380b0e06311Szk 		    &error)) == NULL)
381b0e06311Szk 			goto done;
382b0e06311Szk 
383b0e06311Szk 		/*
384b0e06311Szk 		 * Loop over the returned UDIs to access the raw
385b0e06311Szk 		 * device path.
386b0e06311Szk 		 */
387b0e06311Szk 		for (i = 0; i < ndevs; i++) {
388b0e06311Szk 			if ((path = libhal_device_get_property_string(ctx,
389b0e06311Szk 			    udi[i], HAL_RDSK_PROP, NULL)) != NULL)
390b0e06311Szk 				break;
391b0e06311Szk 			else
392b0e06311Szk 				libhal_free_string(path);
393b0e06311Szk 		}
394b0e06311Szk 
395b0e06311Szk done:
396b0e06311Szk 		if (udi != NULL)
397b0e06311Szk 			libhal_free_string_array(udi);
398b0e06311Szk 		if (dbus_error_is_set(&error))
399b0e06311Szk 			dbus_error_free(&error);
400b0e06311Szk 		detach_from_hald(ctx, HAL_INITIALIZED);
401b0e06311Szk 		return (path);
402b0e06311Szk 	} else {
403b0e06311Szk 		return (NULL);
404b0e06311Szk 	}
405b0e06311Szk }
406b0e06311Szk 
4077c478bd9Sstevel@tonic-gate static int
vol_name_to_dev_node(char * vname,char * found)4087c478bd9Sstevel@tonic-gate vol_name_to_dev_node(char *vname, char *found)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	struct stat statbuf;
4117c478bd9Sstevel@tonic-gate 	char *p1;
4127c478bd9Sstevel@tonic-gate 	int i;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (vname == NULL)
4157c478bd9Sstevel@tonic-gate 		return (0);
416b0e06311Szk 
417b0e06311Szk 	p1 = hald_findname(vname);
418b0e06311Szk 
4197c478bd9Sstevel@tonic-gate 	if (p1 == NULL)
4207c478bd9Sstevel@tonic-gate 		return (0);
4217c478bd9Sstevel@tonic-gate 	if (stat(p1, &statbuf) < 0) {
422b0e06311Szk 		libhal_free_string(p1);
4237c478bd9Sstevel@tonic-gate 		return (0);
4247c478bd9Sstevel@tonic-gate 	}
4254bc0a2efScasper 	if (S_ISDIR(statbuf.st_mode)) {
4267c478bd9Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
4277c478bd9Sstevel@tonic-gate 			(void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
4287c478bd9Sstevel@tonic-gate 			if (access(found, F_OK) >= 0)
4297c478bd9Sstevel@tonic-gate 				break;
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 		if (i == 16) {
432b0e06311Szk 			libhal_free_string(p1);
4337c478bd9Sstevel@tonic-gate 			return (0);
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 	} else {
4367c478bd9Sstevel@tonic-gate 		(void) strlcpy(found, p1, PATH_MAX);
4377c478bd9Sstevel@tonic-gate 	}
438b0e06311Szk 	libhal_free_string(p1);
4397c478bd9Sstevel@tonic-gate 	return (1);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * Builds an open()able device path from a user supplied node which can be
4447c478bd9Sstevel@tonic-gate  * of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
4457c478bd9Sstevel@tonic-gate  * cdrom[n]
4467c478bd9Sstevel@tonic-gate  * returns the path found in 'found' and returns 1. Otherwise returns 0.
4477c478bd9Sstevel@tonic-gate  */
4487c478bd9Sstevel@tonic-gate int
lookup_device(char * supplied,char * found)4497c478bd9Sstevel@tonic-gate lookup_device(char *supplied, char *found)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	struct stat statbuf;
4527c478bd9Sstevel@tonic-gate 	int fd;
4537c478bd9Sstevel@tonic-gate 	char tmpstr[PATH_MAX];
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/* If everything is fine and proper, no need to analyze */
4564bc0a2efScasper 	if ((stat(supplied, &statbuf) == 0) && S_ISCHR(statbuf.st_mode) &&
4577c478bd9Sstevel@tonic-gate 	    ((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
4587c478bd9Sstevel@tonic-gate 		(void) close(fd);
4597c478bd9Sstevel@tonic-gate 		(void) strlcpy(found, supplied, PATH_MAX);
4607c478bd9Sstevel@tonic-gate 		return (1);
4617c478bd9Sstevel@tonic-gate 	}
462b0e06311Szk 
463b0e06311Szk 	/*
464b0e06311Szk 	 * Hal only allows access to a device when the user is
465b0e06311Szk 	 * on the console, therefore if hal is running and we can't
466b0e06311Szk 	 * open the /dev/rdsk or /dev/removable-media/rdsk device
467b0e06311Szk 	 * file we will return 0 marking this device as not avaiable.
468b0e06311Szk 	 */
469b0e06311Szk 	if (fd < 0 && ((strncmp(supplied, "/dev/rdsk/", 10) == 0) ||
470b0e06311Szk 	    (strncmp(supplied, "/dev/removable-media/rdsk/", 26) == 0)))
471b0e06311Szk 		return (0);
472b0e06311Szk 
473b0e06311Szk 	if ((strncmp(supplied, "/dev/dsk/", 9) == 0) ||
474b0e06311Szk 	    (strncmp(supplied, "/dev/removable-media/dsk/", 25) == 0)) {
4757c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
4767c478bd9Sstevel@tonic-gate 		    (char *)strrchr(supplied, '/'));
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
4797c478bd9Sstevel@tonic-gate 			(void) close(fd);
4807c478bd9Sstevel@tonic-gate 			(void) strlcpy(found, supplied, PATH_MAX);
4817c478bd9Sstevel@tonic-gate 			return (1);
4827c478bd9Sstevel@tonic-gate 		}
483b0e06311Szk 
484b0e06311Szk 		/* This device can't be opened mark it as unavailable. */
485b0e06311Szk 		return (0);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	if ((strncmp(supplied, "cdrom", 5) != 0) &&
4887c478bd9Sstevel@tonic-gate 	    (strlen(supplied) < 32)) {
4897c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
4907c478bd9Sstevel@tonic-gate 		    supplied);
4917c478bd9Sstevel@tonic-gate 		if (access(tmpstr, F_OK) < 0) {
4927c478bd9Sstevel@tonic-gate 			(void) strcat(tmpstr, "s2");
4937c478bd9Sstevel@tonic-gate 		}
4947c478bd9Sstevel@tonic-gate 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
4957c478bd9Sstevel@tonic-gate 			(void) close(fd);
4967c478bd9Sstevel@tonic-gate 			(void) strlcpy(found, tmpstr, PATH_MAX);
4977c478bd9Sstevel@tonic-gate 			return (1);
4987c478bd9Sstevel@tonic-gate 		}
499b0e06311Szk 
500b0e06311Szk 		/* This device can't be opened mark it as unavailable. */
501b0e06311Szk 		return (0);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	return (vol_name_to_dev_node(supplied, found));
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate  * Opens the device node name passed and returns 1 (true) if the
5087c478bd9Sstevel@tonic-gate  * device is a CD.
5097c478bd9Sstevel@tonic-gate  */
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate static int
is_cd(char * node)5127c478bd9Sstevel@tonic-gate is_cd(char *node)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	int fd;
5157c478bd9Sstevel@tonic-gate 	struct dk_cinfo cinfo;
5167c478bd9Sstevel@tonic-gate 	int ret = 1;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	fd = open(node, O_RDONLY|O_NDELAY);
5197c478bd9Sstevel@tonic-gate 	if (fd < 0) {
5207c478bd9Sstevel@tonic-gate 		ret = 0;
5217c478bd9Sstevel@tonic-gate 	} else if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
5227c478bd9Sstevel@tonic-gate 		ret = 0;
5237c478bd9Sstevel@tonic-gate 	} else if (cinfo.dki_ctype != DKC_CDROM) {
5247c478bd9Sstevel@tonic-gate 		ret = 0;
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
5287c478bd9Sstevel@tonic-gate 		(void) close(fd);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	return (ret);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate static void
print_header(void)5347c478bd9Sstevel@tonic-gate print_header(void)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : Column spacing should be kept same */
5377c478bd9Sstevel@tonic-gate 	(void) printf(gettext("    Node	           Connected Device"));
5387c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : Column spacing should be kept same */
5397c478bd9Sstevel@tonic-gate 	(void) printf(gettext("	           Device type\n"));
5407c478bd9Sstevel@tonic-gate 	(void) printf(
5417c478bd9Sstevel@tonic-gate 	    "----------------------+--------------------------------");
5427c478bd9Sstevel@tonic-gate 	(void) printf("+-----------------\n");
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate  * returns the number of writers or CD/DVD-roms found and the path of
5477c478bd9Sstevel@tonic-gate  * the first device found depending on the mode argument.
5487c478bd9Sstevel@tonic-gate  * possible mode values are:
5497c478bd9Sstevel@tonic-gate  * SCAN_ALL_CDS 	Scan all CD/DVD devices. Return first CD-RW found.
5507c478bd9Sstevel@tonic-gate  * SCAN_WRITERS		Scan all CD-RW devices. Return first one found.
5517c478bd9Sstevel@tonic-gate  * SCAN_LISTDEVS	List all devices found.
5527c478bd9Sstevel@tonic-gate  */
5537c478bd9Sstevel@tonic-gate int
scan_for_cd_device(int mode,cd_device ** found)5547c478bd9Sstevel@tonic-gate scan_for_cd_device(int mode, cd_device **found)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	DIR *dir;
5577c478bd9Sstevel@tonic-gate 	struct dirent *dirent;
5587c478bd9Sstevel@tonic-gate 	char sdev[PATH_MAX], dev[PATH_MAX];
5597c478bd9Sstevel@tonic-gate 	cd_device *t_dev;
5607c478bd9Sstevel@tonic-gate 	int writers_found = 0;
5617c478bd9Sstevel@tonic-gate 	int header_printed = 0;
5627c478bd9Sstevel@tonic-gate 	int is_writer;
5637c478bd9Sstevel@tonic-gate 	int total_devices_found;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode));
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if (mode) {
5687c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Looking for CD devices...\n"));
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	dir = opendir("/dev/rdsk");
5727c478bd9Sstevel@tonic-gate 	if (dir == NULL)
5737c478bd9Sstevel@tonic-gate 		return (0);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	writers_found = 0;
5767c478bd9Sstevel@tonic-gate 	total_devices_found = 0;
5777c478bd9Sstevel@tonic-gate 	while ((dirent = readdir(dir)) != NULL) {
5787c478bd9Sstevel@tonic-gate 		if (dirent->d_name[0] == '.')
5797c478bd9Sstevel@tonic-gate 			continue;
5807c478bd9Sstevel@tonic-gate 		(void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
5817c478bd9Sstevel@tonic-gate 		    dirent->d_name);
5827c478bd9Sstevel@tonic-gate 		if (strcmp("s2", (char *)strrchr(sdev, 's')) != 0)
5837c478bd9Sstevel@tonic-gate 			continue;
5847c478bd9Sstevel@tonic-gate 		if (!lookup_device(sdev, dev))
5857c478bd9Sstevel@tonic-gate 			continue;
5867c478bd9Sstevel@tonic-gate 		if (!is_cd(dev))
5877c478bd9Sstevel@tonic-gate 			continue;
5887c478bd9Sstevel@tonic-gate 		if ((t_dev = get_device(NULL, dev)) == NULL) {
5897c478bd9Sstevel@tonic-gate 			continue;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		total_devices_found++;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		is_writer = !(check_device(t_dev, CHECK_DEVICE_NOT_WRITABLE));
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		if (is_writer) {
5967c478bd9Sstevel@tonic-gate 			writers_found++;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 			if ((writers_found == 1) && (mode != SCAN_LISTDEVS)) {
5997c478bd9Sstevel@tonic-gate 				*found = t_dev;
6007c478bd9Sstevel@tonic-gate 			}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		} else if ((mode == SCAN_ALL_CDS) && (writers_found == 0) &&
6037c478bd9Sstevel@tonic-gate 		    (total_devices_found == 1) && found) {
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 			/* We found a CD-ROM or DVD-ROM */
6067c478bd9Sstevel@tonic-gate 			*found = t_dev;
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 
609b0e06311Szk 		if (mode == SCAN_LISTDEVS) {
6107c478bd9Sstevel@tonic-gate 			char *sn;
6117c478bd9Sstevel@tonic-gate 
612b0e06311Szk 			sn = hald_symname(sdev);
6137c478bd9Sstevel@tonic-gate 			if (!header_printed) {
6147c478bd9Sstevel@tonic-gate 				print_header();
6157c478bd9Sstevel@tonic-gate 				header_printed = 1;
6167c478bd9Sstevel@tonic-gate 			}
6177c478bd9Sstevel@tonic-gate 			/* show vendor, model, firmware rev and device type */
6187c478bd9Sstevel@tonic-gate 			(void) printf(" %-21.21s| %.8s %.16s %.4s | %s%s\n",
6197c478bd9Sstevel@tonic-gate 			    sn ? sn : sdev, &t_dev->d_inq[8],
6207c478bd9Sstevel@tonic-gate 			    &t_dev->d_inq[16], &t_dev->d_inq[32],
6217c478bd9Sstevel@tonic-gate 			    gettext("CD Reader"),
6227c478bd9Sstevel@tonic-gate 			    is_writer ? gettext("/Writer") : "");
6237c478bd9Sstevel@tonic-gate 			if (sn)
6247c478bd9Sstevel@tonic-gate 				free(sn);
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 		if ((found != NULL) && ((*found) != t_dev))
6277c478bd9Sstevel@tonic-gate 			fini_device(t_dev);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	(void) closedir(dir);
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if ((mode & SCAN_WRITERS) || writers_found)
6337c478bd9Sstevel@tonic-gate 		return (writers_found);
6347c478bd9Sstevel@tonic-gate 	else
6357c478bd9Sstevel@tonic-gate 		return (total_devices_found);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * Check device for various conditions/capabilities
6407c478bd9Sstevel@tonic-gate  * If EXIT_IF_CHECK_FAILED set in cond then it will also exit after
6417c478bd9Sstevel@tonic-gate  * printing a message.
6427c478bd9Sstevel@tonic-gate  */
6437c478bd9Sstevel@tonic-gate int
check_device(cd_device * dev,int cond)6447c478bd9Sstevel@tonic-gate check_device(cd_device *dev, int cond)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	uchar_t *disc_info, disc_status = 0, erasable = 0;
6477c478bd9Sstevel@tonic-gate 	uchar_t page_code[4];
6487c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_TYPE_NOT_CDROM) &&
6517c478bd9Sstevel@tonic-gate 	    ((dev->d_inq[0] & 0x1f) != 5)) {
6527c478bd9Sstevel@tonic-gate 		errmsg =
6537c478bd9Sstevel@tonic-gate 		    gettext("Specified device does not appear to be a CDROM");
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_READY) &&
6577c478bd9Sstevel@tonic-gate 	    !test_unit_ready(dev->d_fd)) {
6587c478bd9Sstevel@tonic-gate 		errmsg = gettext("Device not ready");
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/* Look at the capabilities page for this information */
6627c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_WRITABLE)) {
6637c478bd9Sstevel@tonic-gate 		if (!get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) ||
6647c478bd9Sstevel@tonic-gate 		    ((page_code[3] & 1) == 0)) {
6657c478bd9Sstevel@tonic-gate 			errmsg = gettext("Target device is not a CD writer");
6667c478bd9Sstevel@tonic-gate 		}
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_NO_MEDIA)) {
6707c478bd9Sstevel@tonic-gate 		if (!test_unit_ready(dev->d_fd) && (uscsi_status == 2) &&
6717c478bd9Sstevel@tonic-gate 		    ((RQBUFLEN - rqresid) >= 14) &&
6727c478bd9Sstevel@tonic-gate 		    ((SENSE_KEY(rqbuf) & 0x0f) == 2) && (ASC(rqbuf) == 0x3A) &&
6737c478bd9Sstevel@tonic-gate 		    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
6747c478bd9Sstevel@tonic-gate 		    (ASCQ(rqbuf) == 2))) {
6757c478bd9Sstevel@tonic-gate 			/* medium not present */
6767c478bd9Sstevel@tonic-gate 			errmsg = gettext("No media in device");
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/* Issue READ DISC INFORMATION mmc command */
6837c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && ((cond & CHECK_MEDIA_IS_NOT_BLANK) ||
6847c478bd9Sstevel@tonic-gate 	    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) ||
6857c478bd9Sstevel@tonic-gate 	    (cond & CHECK_MEDIA_IS_NOT_ERASABLE))) {
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		disc_info = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
6887c478bd9Sstevel@tonic-gate 		if (!read_disc_info(dev->d_fd, disc_info)) {
6897c478bd9Sstevel@tonic-gate 			errmsg = gettext("Cannot obtain disc information");
6907c478bd9Sstevel@tonic-gate 		} else {
6917c478bd9Sstevel@tonic-gate 			disc_status = disc_info[2] & 0x03;
6927c478bd9Sstevel@tonic-gate 			erasable = disc_info[2] & 0x10;
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 		free(disc_info);
6957c478bd9Sstevel@tonic-gate 		if (errmsg == NULL) {
6967c478bd9Sstevel@tonic-gate 			if (!erasable && (cond & CHECK_MEDIA_IS_NOT_ERASABLE))
6977c478bd9Sstevel@tonic-gate 				errmsg = gettext(
6987c478bd9Sstevel@tonic-gate 				    "Media in the device is not erasable");
6997c478bd9Sstevel@tonic-gate 			else if ((disc_status != 0) &&
7007c478bd9Sstevel@tonic-gate 			    (cond & CHECK_MEDIA_IS_NOT_BLANK))
7017c478bd9Sstevel@tonic-gate 				errmsg = gettext(
7027c478bd9Sstevel@tonic-gate 				    "Media in the device is not blank");
7037c478bd9Sstevel@tonic-gate 			else if ((disc_status == 2) &&
7047c478bd9Sstevel@tonic-gate 			    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) &&
7057c478bd9Sstevel@tonic-gate 			    ((device_type != DVD_PLUS_W) &&
7067c478bd9Sstevel@tonic-gate 			    (device_type != DVD_PLUS)))
7077c478bd9Sstevel@tonic-gate 				errmsg = gettext(
7087c478bd9Sstevel@tonic-gate 				    "Media in the device is not writable");
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if (errmsg) {
7137c478bd9Sstevel@tonic-gate 		if (cond & EXIT_IF_CHECK_FAILED) {
7147c478bd9Sstevel@tonic-gate 			err_msg("%s.\n", errmsg);
7157c478bd9Sstevel@tonic-gate 			exit(1);
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 		return (1);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	return (0);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate  * Generic routine for writing whatever the next track is and taking
7247c478bd9Sstevel@tonic-gate  * care of the progress bar. Mode tells the track type (audio or data).
7257c478bd9Sstevel@tonic-gate  * Data from track is taken from the byte stream h
7267c478bd9Sstevel@tonic-gate  */
7277c478bd9Sstevel@tonic-gate void
write_next_track(int mode,bstreamhandle h)7287c478bd9Sstevel@tonic-gate write_next_track(int mode, bstreamhandle h)
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	struct track_info *ti;
7317c478bd9Sstevel@tonic-gate 	struct trackio_error *te;
7327c478bd9Sstevel@tonic-gate 	off_t size;
7337c478bd9Sstevel@tonic-gate 
7346e168402Sarutz 	ti = (struct track_info *)my_zalloc(sizeof (*ti));
7357c478bd9Sstevel@tonic-gate 	if ((build_track_info(target, -1, ti) == 0) ||
7367c478bd9Sstevel@tonic-gate 	    ((ti->ti_flags & TI_NWA_VALID) == 0)) {
7377c478bd9Sstevel@tonic-gate 		if ((device_type == DVD_PLUS) || (device_type ==
7387c478bd9Sstevel@tonic-gate 		    DVD_PLUS_W)) {
7397c478bd9Sstevel@tonic-gate 			ti->ti_flags |= TI_NWA_VALID;
7407c478bd9Sstevel@tonic-gate 		} else {
7417c478bd9Sstevel@tonic-gate 			err_msg(gettext(
7427c478bd9Sstevel@tonic-gate 			    "Cannot get writable address for the media.\n"));
7437c478bd9Sstevel@tonic-gate 			exit(1);
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	if (ti->ti_nwa != ti->ti_start_address) {
7477c478bd9Sstevel@tonic-gate 		err_msg(gettext(
7487c478bd9Sstevel@tonic-gate 		    "Media state is not suitable for this write mode.\n"));
7497c478bd9Sstevel@tonic-gate 		exit(1);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 	if (mode == TRACK_MODE_DATA) {
7527c478bd9Sstevel@tonic-gate 		if (!(ti->ti_track_mode & 4)) {
7537c478bd9Sstevel@tonic-gate 			/* Write track depends upon this bit */
7547c478bd9Sstevel@tonic-gate 			ti->ti_track_mode |= TRACK_MODE_DATA;
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 	size = 0;
7587c478bd9Sstevel@tonic-gate 	h->bstr_size(h, &size);
7597c478bd9Sstevel@tonic-gate 	h->bstr_rewind(h);
7607c478bd9Sstevel@tonic-gate 	te = (struct trackio_error *)my_zalloc(sizeof (*te));
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Writing track %d..."), (int)ti->ti_track_no);
7637c478bd9Sstevel@tonic-gate 	init_progress();
7640c83a891Snakanon 	if (!write_track(target, ti, h, progress, size, te)) {
7657c478bd9Sstevel@tonic-gate 		if (te->err_type == TRACKIO_ERR_USER_ABORT) {
7667c478bd9Sstevel@tonic-gate 			(void) str_print(gettext("Aborted.\n"), progress_pos);
7677c478bd9Sstevel@tonic-gate 		} else {
7687c478bd9Sstevel@tonic-gate 			if (device_type != DVD_PLUS_W) {
7697c478bd9Sstevel@tonic-gate 			/* l10n_NOTE : 'failed' as in Writing Track...failed  */
7707c478bd9Sstevel@tonic-gate 				(void) str_print(gettext("failed.\n"),
7717c478bd9Sstevel@tonic-gate 				    progress_pos);
7727c478bd9Sstevel@tonic-gate 			}
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Writing track 1...done"  */
7767c478bd9Sstevel@tonic-gate 	(void) str_print(gettext("done.\n"), progress_pos);
7777c478bd9Sstevel@tonic-gate 	free(ti);
7787c478bd9Sstevel@tonic-gate 	free(te);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate void
list(void)7827c478bd9Sstevel@tonic-gate list(void)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	if (scan_for_cd_device(SCAN_LISTDEVS, NULL) == 0) {
7857c478bd9Sstevel@tonic-gate 		if (vol_running) {
7867c478bd9Sstevel@tonic-gate 			err_msg(gettext(
787b0e06311Szk 			    "No CD writers found, no media in the drive "
788b0e06311Szk 			    "or not on the console.\n"));
7897c478bd9Sstevel@tonic-gate 		} else {
7907c478bd9Sstevel@tonic-gate 			if (cur_uid != 0) {
7917c478bd9Sstevel@tonic-gate 				err_msg(gettext(
7927c478bd9Sstevel@tonic-gate 				    "Volume manager is not running.\n"));
7937c478bd9Sstevel@tonic-gate 				err_msg(gettext(
7947c478bd9Sstevel@tonic-gate "Please start volume manager or run cdrw as root to access all devices.\n"));
7957c478bd9Sstevel@tonic-gate 			} else {
7967c478bd9Sstevel@tonic-gate 				err_msg(gettext("No CD writers found.\n"));
7977c478bd9Sstevel@tonic-gate 			}
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 	exit(0);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate void
get_media_type(int fd)8047c478bd9Sstevel@tonic-gate get_media_type(int fd)
8057c478bd9Sstevel@tonic-gate {
806c9be8e69Sec 	uchar_t *cap = (uchar_t *)my_zalloc(MMC_FTR_HDR_LEN);
8077c478bd9Sstevel@tonic-gate 
808c9be8e69Sec 	if (get_configuration(fd, MMC_FTR_PRFL_LIST,
809c9be8e69Sec 	    MMC_FTR_HDR_LEN, cap)) {
810c9be8e69Sec 		if (debug)
811c9be8e69Sec 			(void) print_profile_list(fd);
812c9be8e69Sec 		switch (read_scsi16(&cap[6])) {
8137c478bd9Sstevel@tonic-gate 			case 0x8: /* CD-ROM */
8147c478bd9Sstevel@tonic-gate 				if (debug)
8157c478bd9Sstevel@tonic-gate 					(void) printf("CD-ROM found\n");
8167c478bd9Sstevel@tonic-gate 				/*
8177c478bd9Sstevel@tonic-gate 				 * To avoid regression issues, treat as
8187c478bd9Sstevel@tonic-gate 				 * A cdrw, we will check the writable
8197c478bd9Sstevel@tonic-gate 				 * mode page to see if the media is
8207c478bd9Sstevel@tonic-gate 				 * actually writable.
8217c478bd9Sstevel@tonic-gate 				 */
8227c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8237c478bd9Sstevel@tonic-gate 				break;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 			case 0x9: /* CD-R */
8267c478bd9Sstevel@tonic-gate 				if (debug)
8277c478bd9Sstevel@tonic-gate 					(void) printf("CD-R found\n");
8287c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8297c478bd9Sstevel@tonic-gate 				break;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 			case 0x10: /* DVD-ROM */
8327c478bd9Sstevel@tonic-gate 				/*
8337c478bd9Sstevel@tonic-gate 				 * Have seen drives return DVD+RW media
8347c478bd9Sstevel@tonic-gate 				 * DVD-ROM, so try treating it as a DVD+RW
8357c478bd9Sstevel@tonic-gate 				 * profile. checking for writable media
8367c478bd9Sstevel@tonic-gate 				 * is done through mode page 5.
8377c478bd9Sstevel@tonic-gate 				 */
8387c478bd9Sstevel@tonic-gate 				if (debug)
8397c478bd9Sstevel@tonic-gate 					(void) printf("DVD-ROM found\n");
8407c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS_W;
8417c478bd9Sstevel@tonic-gate 				break;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 			case 0xA: /* CD-RW */
8447c478bd9Sstevel@tonic-gate 				if (debug)
8457c478bd9Sstevel@tonic-gate 					(void) printf("CD-RW found\n");
8467c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8477c478bd9Sstevel@tonic-gate 				break;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 			case 0x11: /* DVD-R */
8507c478bd9Sstevel@tonic-gate 				if (debug)
8517c478bd9Sstevel@tonic-gate 					(void) printf("DVD-R found\n");
8527c478bd9Sstevel@tonic-gate 				device_type = DVD_MINUS;
8537c478bd9Sstevel@tonic-gate 				break;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 			case 0x12: /* DVD-RAM */
8567c478bd9Sstevel@tonic-gate 				if (debug)
8577c478bd9Sstevel@tonic-gate 					(void) printf("DVD-RAM found\n");
8587c478bd9Sstevel@tonic-gate 				/* treat as CD-RW, may be a legacy drive */
8597c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
8607c478bd9Sstevel@tonic-gate 				break;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 			case 0x13: /* DVD-RW restricted overwrite */
863b0e06311Szk 			case 0x14: /* DVD-RW sequential */
8647c478bd9Sstevel@tonic-gate 				if (debug)
8657c478bd9Sstevel@tonic-gate 					(void) printf("DVD-RW found\n");
8667c478bd9Sstevel@tonic-gate 				device_type = DVD_MINUS;
8677c478bd9Sstevel@tonic-gate 				break;
8687c478bd9Sstevel@tonic-gate 
869aefd6f19Szk 			case 0x15: /* DVD-R Dual Layer Sequential Recording */
870aefd6f19Szk 			case 0x16: /* DVD-R Dual Layer Jump Recording */
871aefd6f19Szk 				if (debug)
872aefd6f19Szk 					(void) printf("DVD-R DL found\n");
873aefd6f19Szk 				device_type = DVD_MINUS;
874aefd6f19Szk 				break;
875aefd6f19Szk 
876aefd6f19Szk 			case 0x17: /* DVD-RW Dual Layer */
877aefd6f19Szk 				if (debug)
878aefd6f19Szk 					(void) printf("DVD-RW DL found\n");
879aefd6f19Szk 				device_type = DVD_MINUS;
880aefd6f19Szk 				break;
881aefd6f19Szk 
8827c478bd9Sstevel@tonic-gate 			case 0x1A: /* DVD+RW */
8837c478bd9Sstevel@tonic-gate 				if (debug)
8847c478bd9Sstevel@tonic-gate 					(void) printf("DVD+RW found\n");
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS_W;
8877c478bd9Sstevel@tonic-gate 				break;
888aefd6f19Szk 
8897c478bd9Sstevel@tonic-gate 			case 0x1B: /* DVD+R */
8907c478bd9Sstevel@tonic-gate 				if (debug)
8917c478bd9Sstevel@tonic-gate 					(void) printf("DVD+R found\n");
8927c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS;
8937c478bd9Sstevel@tonic-gate 				break;
8947c478bd9Sstevel@tonic-gate 
895aefd6f19Szk 			case 0x2A: /* DVD+RW Dual Layer */
896aefd6f19Szk 				if (debug)
897aefd6f19Szk 					(void) printf("DVD+RW DL found\n");
898aefd6f19Szk 				device_type = DVD_PLUS_W;
899aefd6f19Szk 				break;
900aefd6f19Szk 
901aefd6f19Szk 			case 0x2B: /* DVD+R Dual Layer */
902aefd6f19Szk 				if (debug)
903aefd6f19Szk 					(void) printf("DVD+R DL found\n");
904aefd6f19Szk 				device_type = DVD_PLUS;
905aefd6f19Szk 				break;
906aefd6f19Szk 
9077c478bd9Sstevel@tonic-gate 			default:
9087c478bd9Sstevel@tonic-gate 				if (debug)
9097c478bd9Sstevel@tonic-gate 					(void) printf(
910b0e06311Szk 					    "unknown drive found\n type = 0x%x",
911b0e06311Szk 					    cap[7]);
9127c478bd9Sstevel@tonic-gate 				/*
9137c478bd9Sstevel@tonic-gate 				 * Treat as CD_RW to avoid regression, may
9147c478bd9Sstevel@tonic-gate 				 * be a legacy drive.
9157c478bd9Sstevel@tonic-gate 				 */
9167c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 	}
919c9be8e69Sec 	free(cap);
9207c478bd9Sstevel@tonic-gate }
92198584592Sarutz 
92298584592Sarutz /* Translate a transfer rate (eg, KB/s) into a Speed (eg, "2X") */
92398584592Sarutz uint_t
cdrw_bandwidth_to_x(uint_t rate)92498584592Sarutz cdrw_bandwidth_to_x(uint_t rate)
92598584592Sarutz {
92698584592Sarutz 	switch (device_type) {
92798584592Sarutz 	case DVD_PLUS_W:
92898584592Sarutz 	case DVD_MINUS:
92998584592Sarutz 	case DVD_PLUS:
93098584592Sarutz 		return (DVD_RATE_TO_X(rate));
93198584592Sarutz 
93298584592Sarutz 	default:
93398584592Sarutz 	case CD_RW:
93498584592Sarutz 		return (CD_RATE_TO_X(rate));
93598584592Sarutz 	}
93698584592Sarutz }
93798584592Sarutz 
93898584592Sarutz /* Translate a Speed (eg, "2X") into a transfer rate (eg, KB/s) */
93998584592Sarutz uint_t
cdrw_x_to_bandwidth(uint_t x)94098584592Sarutz cdrw_x_to_bandwidth(uint_t x)
94198584592Sarutz {
94298584592Sarutz 	switch (device_type) {
94398584592Sarutz 	case DVD_PLUS_W:
94498584592Sarutz 	case DVD_MINUS:
94598584592Sarutz 	case DVD_PLUS:
94698584592Sarutz 		return (DVD_X_TO_RATE(x));
94798584592Sarutz 
94898584592Sarutz 	default:
94998584592Sarutz 	case CD_RW:
95098584592Sarutz 		return (CD_X_TO_RATE(x));
95198584592Sarutz 	}
95298584592Sarutz }
953