xref: /titanic_51/usr/src/cmd/cdrw/device.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
31*7c478bd9Sstevel@tonic-gate #include <volmgt.h>
32*7c478bd9Sstevel@tonic-gate #include <errno.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
35*7c478bd9Sstevel@tonic-gate #include <unistd.h>
36*7c478bd9Sstevel@tonic-gate #include <dirent.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <libintl.h>
40*7c478bd9Sstevel@tonic-gate #include <limits.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include "transport.h"
43*7c478bd9Sstevel@tonic-gate #include "mmc.h"
44*7c478bd9Sstevel@tonic-gate #include "device.h"
45*7c478bd9Sstevel@tonic-gate #include "util.h"
46*7c478bd9Sstevel@tonic-gate #include "msgs.h"
47*7c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
48*7c478bd9Sstevel@tonic-gate #include "toshiba.h"
49*7c478bd9Sstevel@tonic-gate #include "main.h"
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate /*
52*7c478bd9Sstevel@tonic-gate  * Old sun drives have a vendor specific mode page for setting/getting speed.
53*7c478bd9Sstevel@tonic-gate  * Also they use a different method for extracting audio.
54*7c478bd9Sstevel@tonic-gate  * We have the device inquiry strings at this time. This is used to enable
55*7c478bd9Sstevel@tonic-gate  * us to use older sun drives to extract audio.
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate static int
58*7c478bd9Sstevel@tonic-gate is_old_sun_drive(cd_device *dev)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	/*
61*7c478bd9Sstevel@tonic-gate 	 * If we have a SONY CDU 561, CDU 8012, or TOSHIBA model with XMa we
62*7c478bd9Sstevel@tonic-gate 	 * need to handle these drives a bit differently.
63*7c478bd9Sstevel@tonic-gate 	 */
64*7c478bd9Sstevel@tonic-gate 	if (strncmp("SONY", (const char *)&dev->d_inq[8], 4) == 0) {
65*7c478bd9Sstevel@tonic-gate 		if (strncmp("CDU 561", (const char *)&dev->d_inq[16], 7) == 0)
66*7c478bd9Sstevel@tonic-gate 			return (1);
67*7c478bd9Sstevel@tonic-gate 		if (strncmp("CDU-8012", (const char *)&dev->d_inq[16], 8) == 0)
68*7c478bd9Sstevel@tonic-gate 			return (1);
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	if ((strncmp("TOSHIBA", (const char *)&dev->d_inq[8], 7) == 0) &&
72*7c478bd9Sstevel@tonic-gate 	    (strncmp("XM", (const char *)&dev->d_inq[16], 2) == 0)) {
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 		char product_id[17];
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 		/* Changing speed is not allowed for 32X TOSHIBA drives */
77*7c478bd9Sstevel@tonic-gate 		if (strncmp("SUN32XCD", (const char *)&dev->d_inq[24], 8) == 0)
78*7c478bd9Sstevel@tonic-gate 			dev->d_cap |= DEV_CAP_SETTING_SPEED_NOT_ALLOWED;
79*7c478bd9Sstevel@tonic-gate 		(void) strncpy(product_id, (const char *)&dev->d_inq[16], 16);
80*7c478bd9Sstevel@tonic-gate 		product_id[16] = 0;
81*7c478bd9Sstevel@tonic-gate 		if (strstr(product_id, "SUN") != NULL)
82*7c478bd9Sstevel@tonic-gate 			return (1);
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 	return (0);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate  * returns a cd_device handle for a node returned by lookup_device()
89*7c478bd9Sstevel@tonic-gate  * also takes the user supplied name and stores it inside the node
90*7c478bd9Sstevel@tonic-gate  */
91*7c478bd9Sstevel@tonic-gate cd_device *
92*7c478bd9Sstevel@tonic-gate get_device(char *user_supplied, char *node)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	cd_device *dev;
95*7c478bd9Sstevel@tonic-gate 	int fd;
96*7c478bd9Sstevel@tonic-gate 	uchar_t *cap;
97*7c478bd9Sstevel@tonic-gate 	char devnode[PATH_MAX];
98*7c478bd9Sstevel@tonic-gate 	int size;
99*7c478bd9Sstevel@tonic-gate 	struct dk_minfo mediainfo;
100*7c478bd9Sstevel@tonic-gate 	int use_cd_speed = 0;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	/*
103*7c478bd9Sstevel@tonic-gate 	 * we need to resolve any link paths to avoid fake files
104*7c478bd9Sstevel@tonic-gate 	 * such as /dev/rdsk/../../export/file.
105*7c478bd9Sstevel@tonic-gate 	 */
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("get_device(%s, %s)\n", user_supplied ?
108*7c478bd9Sstevel@tonic-gate 	    user_supplied : "<nil>", node ? node : "<nil>"));
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	size = resolvepath(node, devnode, PATH_MAX);
111*7c478bd9Sstevel@tonic-gate 	if ((size <= 0) || (size >= (PATH_MAX - 1)))
112*7c478bd9Sstevel@tonic-gate 		return (NULL);
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	/* resolvepath may not return a null terminated string */
115*7c478bd9Sstevel@tonic-gate 	devnode[size] = '\0';
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	/* the device node must be in /devices/ or /vol/dev/rdsk */
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	if ((strncmp(devnode, "/devices/", 9) != 0) &&
121*7c478bd9Sstevel@tonic-gate 	    (strncmp(devnode, "/vol/dev/rdsk", 13) != 0))
122*7c478bd9Sstevel@tonic-gate 		return (NULL);
123*7c478bd9Sstevel@tonic-gate 	/*
124*7c478bd9Sstevel@tonic-gate 	 * Since we are currently running with the user euid it is
125*7c478bd9Sstevel@tonic-gate 	 * safe to try to open the file without checking access.
126*7c478bd9Sstevel@tonic-gate 	 */
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	fd = open(devnode, O_RDONLY|O_NDELAY);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (fd < 0) {
131*7c478bd9Sstevel@tonic-gate 		TRACE(traceall_msg("Cannot open %s: %s\n", node,
132*7c478bd9Sstevel@tonic-gate 		    strerror(errno)));
133*7c478bd9Sstevel@tonic-gate 		return (NULL);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	dev = (cd_device *)my_zalloc(sizeof (cd_device));
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	dev->d_node = (char *)my_zalloc(strlen(devnode) + 1);
139*7c478bd9Sstevel@tonic-gate 	(void) strcpy(dev->d_node, devnode);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	dev->d_fd = fd;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	dev->d_inq = (uchar_t *)my_zalloc(INQUIRY_DATA_LENGTH);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	if (!inquiry(fd, dev->d_inq)) {
146*7c478bd9Sstevel@tonic-gate 		TRACE(traceall_msg("Inquiry failed on device %s\n", node));
147*7c478bd9Sstevel@tonic-gate 		if (debug) {
148*7c478bd9Sstevel@tonic-gate 			(void) printf("USCSI ioctl failed %d\n",
149*7c478bd9Sstevel@tonic-gate 			    uscsi_error);
150*7c478bd9Sstevel@tonic-gate 		}
151*7c478bd9Sstevel@tonic-gate 		free(dev->d_inq);
152*7c478bd9Sstevel@tonic-gate 		free(dev->d_node);
153*7c478bd9Sstevel@tonic-gate 		(void) close(dev->d_fd);
154*7c478bd9Sstevel@tonic-gate 		free(dev);
155*7c478bd9Sstevel@tonic-gate 		return (NULL);
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if (debug) {
159*7c478bd9Sstevel@tonic-gate 		cap = (uchar_t *)my_zalloc(18);
160*7c478bd9Sstevel@tonic-gate 		(void) printf("Checking device type\n");
161*7c478bd9Sstevel@tonic-gate 		if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
162*7c478bd9Sstevel@tonic-gate 			if (cap[2] & 0x10)
163*7c478bd9Sstevel@tonic-gate 				(void) printf("DVD-R read support\n");
164*7c478bd9Sstevel@tonic-gate 			if (cap[3] & 0x10)
165*7c478bd9Sstevel@tonic-gate 				(void) printf("DVD-R write support\n");
166*7c478bd9Sstevel@tonic-gate 			if (cap[5] & 0x4)
167*7c478bd9Sstevel@tonic-gate 				(void) printf("R-W supported\n");
168*7c478bd9Sstevel@tonic-gate 			if (cap[2] & 0x20)
169*7c478bd9Sstevel@tonic-gate 				(void) printf("DVD-RAM read supported\n");
170*7c478bd9Sstevel@tonic-gate 			if (cap[3] & 0x20)
171*7c478bd9Sstevel@tonic-gate 				(void) printf("DVD-RAM write supported\n");
172*7c478bd9Sstevel@tonic-gate 		} else {
173*7c478bd9Sstevel@tonic-gate 			(void) printf("Could not read mode page 2A! \n");
174*7c478bd9Sstevel@tonic-gate 		}
175*7c478bd9Sstevel@tonic-gate 		free(cap);
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	/* Detect if it's a Lite-ON drive with a streaming CD problem */
179*7c478bd9Sstevel@tonic-gate 	if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
180*7c478bd9Sstevel@tonic-gate 	    (strncmp("LTR-48", (const char *)&dev->d_inq[16], 6) == 0)) {
181*7c478bd9Sstevel@tonic-gate 		use_cd_speed = 1;
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	cap = (uchar_t *)my_zalloc(8);
185*7c478bd9Sstevel@tonic-gate 	if (is_old_sun_drive(dev)) {
186*7c478bd9Sstevel@tonic-gate 		dev->d_read_audio = toshiba_read_audio;
187*7c478bd9Sstevel@tonic-gate 		dev->d_speed_ctrl = toshiba_speed_ctrl;
188*7c478bd9Sstevel@tonic-gate 	} else if (use_cd_speed)  {
189*7c478bd9Sstevel@tonic-gate 		/*
190*7c478bd9Sstevel@tonic-gate 		 * Work around for Lite-on FW bug in which rt_speed_ctrl
191*7c478bd9Sstevel@tonic-gate 		 * doesn't work correctly.
192*7c478bd9Sstevel@tonic-gate 		 */
193*7c478bd9Sstevel@tonic-gate 		dev->d_speed_ctrl = cd_speed_ctrl;
194*7c478bd9Sstevel@tonic-gate 	} else {
195*7c478bd9Sstevel@tonic-gate 		/*
196*7c478bd9Sstevel@tonic-gate 		 * If feature 8 (see GET CONF MMC command) is supported,
197*7c478bd9Sstevel@tonic-gate 		 * READ CD will work and will return jitter free audio data.
198*7c478bd9Sstevel@tonic-gate 		 * Otherwise look at page code 2A for this info.
199*7c478bd9Sstevel@tonic-gate 		 */
200*7c478bd9Sstevel@tonic-gate 		if (get_configuration(fd, 0x1E, 8, cap)) {
201*7c478bd9Sstevel@tonic-gate 			dev->d_read_audio = read_audio_through_read_cd;
202*7c478bd9Sstevel@tonic-gate 			dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
203*7c478bd9Sstevel@tonic-gate 		} else if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
204*7c478bd9Sstevel@tonic-gate 			if (cap[5] & 1) {
205*7c478bd9Sstevel@tonic-gate 				dev->d_read_audio = read_audio_through_read_cd;
206*7c478bd9Sstevel@tonic-gate 				if (cap[5] & 2)
207*7c478bd9Sstevel@tonic-gate 					dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
208*7c478bd9Sstevel@tonic-gate 			}
209*7c478bd9Sstevel@tonic-gate 		}
210*7c478bd9Sstevel@tonic-gate 		/*
211*7c478bd9Sstevel@tonic-gate 		 * If feature 0x0107 is suported then Real-time streaming
212*7c478bd9Sstevel@tonic-gate 		 * commands can be used for speed control. Otherwise try
213*7c478bd9Sstevel@tonic-gate 		 * SET CD SPEED.
214*7c478bd9Sstevel@tonic-gate 		 */
215*7c478bd9Sstevel@tonic-gate 		if (get_configuration(fd, 0x0107, 8, cap)) {
216*7c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = rt_streaming_ctrl;
217*7c478bd9Sstevel@tonic-gate 			if (debug)
218*7c478bd9Sstevel@tonic-gate 				(void) printf("using rt speed ctrl\n");
219*7c478bd9Sstevel@tonic-gate 		} else {
220*7c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = cd_speed_ctrl;
221*7c478bd9Sstevel@tonic-gate 			if (debug)
222*7c478bd9Sstevel@tonic-gate 				(void) printf("using cd speed ctrl\n");
223*7c478bd9Sstevel@tonic-gate 		}
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 	if (dev->d_read_audio != NULL)
226*7c478bd9Sstevel@tonic-gate 		dev->d_cap |= DEV_CAP_EXTRACT_CDDA;
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	dev->d_blksize = 0;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	/*
231*7c478bd9Sstevel@tonic-gate 	 * Find the block size of the device so we can translate
232*7c478bd9Sstevel@tonic-gate 	 * the reads/writes to the device blocksize.
233*7c478bd9Sstevel@tonic-gate 	 */
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGMEDIAINFO, &mediainfo) < 0) {
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 		/*
238*7c478bd9Sstevel@tonic-gate 		 * If DKIOCGMEDIAINFO fails we'll try to get
239*7c478bd9Sstevel@tonic-gate 		 * the blocksize from the device itself.
240*7c478bd9Sstevel@tonic-gate 		 */
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		if (debug)
243*7c478bd9Sstevel@tonic-gate 			(void) printf("DKIOCGMEDIAINFO failed\n");
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 		if (read_capacity(fd, cap))
246*7c478bd9Sstevel@tonic-gate 			dev->d_blksize = read_scsi32(cap + 4);
247*7c478bd9Sstevel@tonic-gate 	} else {
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		dev->d_blksize = mediainfo.dki_lbsize;
250*7c478bd9Sstevel@tonic-gate 	}
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	if (debug) {
253*7c478bd9Sstevel@tonic-gate 		uint_t bsize;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 		(void) printf("blocksize = %d\n", dev->d_blksize);
256*7c478bd9Sstevel@tonic-gate 		(void) printf("read_format_capacity = %d \n",
257*7c478bd9Sstevel@tonic-gate 		    read_format_capacity(fd, &bsize));
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	/*
261*7c478bd9Sstevel@tonic-gate 	 * Some devices will return invalid blocksizes. ie. Toshiba
262*7c478bd9Sstevel@tonic-gate 	 * drives will return 2352 when an audio CD is inserted.
263*7c478bd9Sstevel@tonic-gate 	 * Older Sun drives will use 512 byte block sizes. All newer
264*7c478bd9Sstevel@tonic-gate 	 * drives should have 2k blocksizes.
265*7c478bd9Sstevel@tonic-gate 	 */
266*7c478bd9Sstevel@tonic-gate 	if (((dev->d_blksize != 512) && (dev->d_blksize != 2048))) {
267*7c478bd9Sstevel@tonic-gate 			if (is_old_sun_drive(dev)) {
268*7c478bd9Sstevel@tonic-gate 				dev->d_blksize = 512;
269*7c478bd9Sstevel@tonic-gate 			} else {
270*7c478bd9Sstevel@tonic-gate 				dev->d_blksize = 2048;
271*7c478bd9Sstevel@tonic-gate 			}
272*7c478bd9Sstevel@tonic-gate 		if (debug)
273*7c478bd9Sstevel@tonic-gate 			(void) printf(" switching to %d\n", dev->d_blksize);
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	free(cap);
277*7c478bd9Sstevel@tonic-gate 	if (user_supplied) {
278*7c478bd9Sstevel@tonic-gate 		dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
279*7c478bd9Sstevel@tonic-gate 		(void) strcpy(dev->d_name, user_supplied);
280*7c478bd9Sstevel@tonic-gate 	}
281*7c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("Got device %s\n", node));
282*7c478bd9Sstevel@tonic-gate 	return (dev);
283*7c478bd9Sstevel@tonic-gate }
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate void
286*7c478bd9Sstevel@tonic-gate fini_device(cd_device *dev)
287*7c478bd9Sstevel@tonic-gate {
288*7c478bd9Sstevel@tonic-gate 	free(dev->d_inq);
289*7c478bd9Sstevel@tonic-gate 	free(dev->d_node);
290*7c478bd9Sstevel@tonic-gate 	(void) close(dev->d_fd);
291*7c478bd9Sstevel@tonic-gate 	if (dev->d_name)
292*7c478bd9Sstevel@tonic-gate 		free(dev->d_name);
293*7c478bd9Sstevel@tonic-gate 	free(dev);
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate static int
297*7c478bd9Sstevel@tonic-gate vol_name_to_dev_node(char *vname, char *found)
298*7c478bd9Sstevel@tonic-gate {
299*7c478bd9Sstevel@tonic-gate 	struct stat statbuf;
300*7c478bd9Sstevel@tonic-gate 	char *p1;
301*7c478bd9Sstevel@tonic-gate 	int i;
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	if (vname == NULL)
304*7c478bd9Sstevel@tonic-gate 		return (0);
305*7c478bd9Sstevel@tonic-gate 	if (vol_running)
306*7c478bd9Sstevel@tonic-gate 		(void) volmgt_check(vname);
307*7c478bd9Sstevel@tonic-gate 	p1 = media_findname(vname);
308*7c478bd9Sstevel@tonic-gate 	if (p1 == NULL)
309*7c478bd9Sstevel@tonic-gate 		return (0);
310*7c478bd9Sstevel@tonic-gate 	if (stat(p1, &statbuf) < 0) {
311*7c478bd9Sstevel@tonic-gate 		free(p1);
312*7c478bd9Sstevel@tonic-gate 		return (0);
313*7c478bd9Sstevel@tonic-gate 	}
314*7c478bd9Sstevel@tonic-gate 	if (statbuf.st_mode & S_IFDIR) {
315*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
316*7c478bd9Sstevel@tonic-gate 			(void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
317*7c478bd9Sstevel@tonic-gate 			if (access(found, F_OK) >= 0)
318*7c478bd9Sstevel@tonic-gate 				break;
319*7c478bd9Sstevel@tonic-gate 		}
320*7c478bd9Sstevel@tonic-gate 		if (i == 16) {
321*7c478bd9Sstevel@tonic-gate 			free(p1);
322*7c478bd9Sstevel@tonic-gate 			return (0);
323*7c478bd9Sstevel@tonic-gate 		}
324*7c478bd9Sstevel@tonic-gate 	} else {
325*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(found, p1, PATH_MAX);
326*7c478bd9Sstevel@tonic-gate 	}
327*7c478bd9Sstevel@tonic-gate 	free(p1);
328*7c478bd9Sstevel@tonic-gate 	return (1);
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate /*
332*7c478bd9Sstevel@tonic-gate  * Searches for volume manager's equivalent char device for the
333*7c478bd9Sstevel@tonic-gate  * supplied pathname which is of the form of /dev/rdsk/cxtxdxsx
334*7c478bd9Sstevel@tonic-gate  */
335*7c478bd9Sstevel@tonic-gate static int
336*7c478bd9Sstevel@tonic-gate vol_lookup(char *supplied, char *found)
337*7c478bd9Sstevel@tonic-gate {
338*7c478bd9Sstevel@tonic-gate 	char tmpstr[PATH_MAX], tmpstr1[PATH_MAX], *p;
339*7c478bd9Sstevel@tonic-gate 	int i, ret;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpstr, supplied, PATH_MAX);
342*7c478bd9Sstevel@tonic-gate 	if ((p = volmgt_symname(tmpstr)) == NULL) {
343*7c478bd9Sstevel@tonic-gate 		if (strrchr(tmpstr, 's') == NULL)
344*7c478bd9Sstevel@tonic-gate 			return (0);
345*7c478bd9Sstevel@tonic-gate 		*((char *)(strrchr(tmpstr, 's') + 1)) = 0;
346*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
347*7c478bd9Sstevel@tonic-gate 			(void) snprintf(tmpstr1, PATH_MAX, "%s%d", tmpstr, i);
348*7c478bd9Sstevel@tonic-gate 			if ((p = volmgt_symname(tmpstr1)) != NULL)
349*7c478bd9Sstevel@tonic-gate 				break;
350*7c478bd9Sstevel@tonic-gate 		}
351*7c478bd9Sstevel@tonic-gate 		if (p == NULL)
352*7c478bd9Sstevel@tonic-gate 			return (0);
353*7c478bd9Sstevel@tonic-gate 	}
354*7c478bd9Sstevel@tonic-gate 	ret = vol_name_to_dev_node(p, found);
355*7c478bd9Sstevel@tonic-gate 	free(p);
356*7c478bd9Sstevel@tonic-gate 	return (ret);
357*7c478bd9Sstevel@tonic-gate }
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate /*
360*7c478bd9Sstevel@tonic-gate  * Builds an open()able device path from a user supplied node which can be
361*7c478bd9Sstevel@tonic-gate  * of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
362*7c478bd9Sstevel@tonic-gate  * cdrom[n]
363*7c478bd9Sstevel@tonic-gate  * returns the path found in 'found' and returns 1. Otherwise returns 0.
364*7c478bd9Sstevel@tonic-gate  */
365*7c478bd9Sstevel@tonic-gate int
366*7c478bd9Sstevel@tonic-gate lookup_device(char *supplied, char *found)
367*7c478bd9Sstevel@tonic-gate {
368*7c478bd9Sstevel@tonic-gate 	struct stat statbuf;
369*7c478bd9Sstevel@tonic-gate 	int fd;
370*7c478bd9Sstevel@tonic-gate 	char tmpstr[PATH_MAX];
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	/* If everything is fine and proper, no need to analyze */
373*7c478bd9Sstevel@tonic-gate 	if ((stat(supplied, &statbuf) == 0) && (statbuf.st_mode & S_IFCHR) &&
374*7c478bd9Sstevel@tonic-gate 	    ((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
375*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
376*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(found, supplied, PATH_MAX);
377*7c478bd9Sstevel@tonic-gate 		return (1);
378*7c478bd9Sstevel@tonic-gate 	}
379*7c478bd9Sstevel@tonic-gate 	if (strncmp(supplied, "/dev/rdsk/", 10) == 0)
380*7c478bd9Sstevel@tonic-gate 		return (vol_lookup(supplied, found));
381*7c478bd9Sstevel@tonic-gate 	if (strncmp(supplied, "/dev/dsk/", 9) == 0) {
382*7c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
383*7c478bd9Sstevel@tonic-gate 		    (char *)strrchr(supplied, '/'));
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
386*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
387*7c478bd9Sstevel@tonic-gate 			(void) strlcpy(found, supplied, PATH_MAX);
388*7c478bd9Sstevel@tonic-gate 			return (1);
389*7c478bd9Sstevel@tonic-gate 		}
390*7c478bd9Sstevel@tonic-gate 		if ((access(tmpstr, F_OK) == 0) && vol_running)
391*7c478bd9Sstevel@tonic-gate 			return (vol_lookup(tmpstr, found));
392*7c478bd9Sstevel@tonic-gate 		else
393*7c478bd9Sstevel@tonic-gate 			return (0);
394*7c478bd9Sstevel@tonic-gate 	}
395*7c478bd9Sstevel@tonic-gate 	if ((strncmp(supplied, "cdrom", 5) != 0) &&
396*7c478bd9Sstevel@tonic-gate 	    (strlen(supplied) < 32)) {
397*7c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
398*7c478bd9Sstevel@tonic-gate 		    supplied);
399*7c478bd9Sstevel@tonic-gate 		if (access(tmpstr, F_OK) < 0) {
400*7c478bd9Sstevel@tonic-gate 			(void) strcat(tmpstr, "s2");
401*7c478bd9Sstevel@tonic-gate 		}
402*7c478bd9Sstevel@tonic-gate 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
403*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
404*7c478bd9Sstevel@tonic-gate 			(void) strlcpy(found, tmpstr, PATH_MAX);
405*7c478bd9Sstevel@tonic-gate 			return (1);
406*7c478bd9Sstevel@tonic-gate 		}
407*7c478bd9Sstevel@tonic-gate 		if ((access(tmpstr, F_OK) == 0) && vol_running)
408*7c478bd9Sstevel@tonic-gate 			return (vol_lookup(tmpstr, found));
409*7c478bd9Sstevel@tonic-gate 	}
410*7c478bd9Sstevel@tonic-gate 	return (vol_name_to_dev_node(supplied, found));
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate /*
414*7c478bd9Sstevel@tonic-gate  * Opens the device node name passed and returns 1 (true) if the
415*7c478bd9Sstevel@tonic-gate  * device is a CD.
416*7c478bd9Sstevel@tonic-gate  */
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate static int
419*7c478bd9Sstevel@tonic-gate is_cd(char *node)
420*7c478bd9Sstevel@tonic-gate {
421*7c478bd9Sstevel@tonic-gate 	int fd;
422*7c478bd9Sstevel@tonic-gate 	struct dk_cinfo cinfo;
423*7c478bd9Sstevel@tonic-gate 	int ret = 1;
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	fd = open(node, O_RDONLY|O_NDELAY);
426*7c478bd9Sstevel@tonic-gate 	if (fd < 0) {
427*7c478bd9Sstevel@tonic-gate 		ret = 0;
428*7c478bd9Sstevel@tonic-gate 	} else if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
429*7c478bd9Sstevel@tonic-gate 		ret = 0;
430*7c478bd9Sstevel@tonic-gate 	} else if (cinfo.dki_ctype != DKC_CDROM) {
431*7c478bd9Sstevel@tonic-gate 		ret = 0;
432*7c478bd9Sstevel@tonic-gate 	}
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
435*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
436*7c478bd9Sstevel@tonic-gate 	}
437*7c478bd9Sstevel@tonic-gate 	return (ret);
438*7c478bd9Sstevel@tonic-gate }
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate static void
441*7c478bd9Sstevel@tonic-gate print_header(void)
442*7c478bd9Sstevel@tonic-gate {
443*7c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : Column spacing should be kept same */
444*7c478bd9Sstevel@tonic-gate 	(void) printf(gettext("    Node	           Connected Device"));
445*7c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : Column spacing should be kept same */
446*7c478bd9Sstevel@tonic-gate 	(void) printf(gettext("	           Device type\n"));
447*7c478bd9Sstevel@tonic-gate 	(void) printf(
448*7c478bd9Sstevel@tonic-gate 	    "----------------------+--------------------------------");
449*7c478bd9Sstevel@tonic-gate 	(void) printf("+-----------------\n");
450*7c478bd9Sstevel@tonic-gate }
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate /*
453*7c478bd9Sstevel@tonic-gate  * returns the number of writers or CD/DVD-roms found and the path of
454*7c478bd9Sstevel@tonic-gate  * the first device found depending on the mode argument.
455*7c478bd9Sstevel@tonic-gate  * possible mode values are:
456*7c478bd9Sstevel@tonic-gate  * SCAN_ALL_CDS 	Scan all CD/DVD devices. Return first CD-RW found.
457*7c478bd9Sstevel@tonic-gate  * SCAN_WRITERS		Scan all CD-RW devices. Return first one found.
458*7c478bd9Sstevel@tonic-gate  * SCAN_LISTDEVS	List all devices found.
459*7c478bd9Sstevel@tonic-gate  */
460*7c478bd9Sstevel@tonic-gate int
461*7c478bd9Sstevel@tonic-gate scan_for_cd_device(int mode, cd_device **found)
462*7c478bd9Sstevel@tonic-gate {
463*7c478bd9Sstevel@tonic-gate 	DIR *dir;
464*7c478bd9Sstevel@tonic-gate 	struct dirent *dirent;
465*7c478bd9Sstevel@tonic-gate 	char sdev[PATH_MAX], dev[PATH_MAX];
466*7c478bd9Sstevel@tonic-gate 	cd_device *t_dev;
467*7c478bd9Sstevel@tonic-gate 	int writers_found = 0;
468*7c478bd9Sstevel@tonic-gate 	int header_printed = 0;
469*7c478bd9Sstevel@tonic-gate 	int is_writer;
470*7c478bd9Sstevel@tonic-gate 	int retry = 0;
471*7c478bd9Sstevel@tonic-gate 	int total_devices_found;
472*7c478bd9Sstevel@tonic-gate 	int total_devices_found_last_time = 0;
473*7c478bd9Sstevel@tonic-gate 	int defer = 0;
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate #define	MAX_RETRIES_FOR_SCANNING 5
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 	TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode));
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	if (mode) {
480*7c478bd9Sstevel@tonic-gate 		if (vol_running)
481*7c478bd9Sstevel@tonic-gate 			defer = 1;
482*7c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Looking for CD devices...\n"));
483*7c478bd9Sstevel@tonic-gate 	}
484*7c478bd9Sstevel@tonic-gate try_again:
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	dir = opendir("/dev/rdsk");
487*7c478bd9Sstevel@tonic-gate 	if (dir == NULL)
488*7c478bd9Sstevel@tonic-gate 		return (0);
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	writers_found = 0;
491*7c478bd9Sstevel@tonic-gate 	total_devices_found = 0;
492*7c478bd9Sstevel@tonic-gate 	while ((dirent = readdir(dir)) != NULL) {
493*7c478bd9Sstevel@tonic-gate 		if (dirent->d_name[0] == '.')
494*7c478bd9Sstevel@tonic-gate 			continue;
495*7c478bd9Sstevel@tonic-gate 		(void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
496*7c478bd9Sstevel@tonic-gate 		    dirent->d_name);
497*7c478bd9Sstevel@tonic-gate 		if (strcmp("s2", (char *)strrchr(sdev, 's')) != 0)
498*7c478bd9Sstevel@tonic-gate 			continue;
499*7c478bd9Sstevel@tonic-gate 		if (!lookup_device(sdev, dev))
500*7c478bd9Sstevel@tonic-gate 			continue;
501*7c478bd9Sstevel@tonic-gate 		if (!is_cd(dev))
502*7c478bd9Sstevel@tonic-gate 			continue;
503*7c478bd9Sstevel@tonic-gate 		if ((t_dev = get_device(NULL, dev)) == NULL) {
504*7c478bd9Sstevel@tonic-gate 			continue;
505*7c478bd9Sstevel@tonic-gate 		}
506*7c478bd9Sstevel@tonic-gate 		total_devices_found++;
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate 		is_writer = !(check_device(t_dev, CHECK_DEVICE_NOT_WRITABLE));
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 		if (is_writer) {
511*7c478bd9Sstevel@tonic-gate 			writers_found++;
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 			if ((writers_found == 1) && (mode != SCAN_LISTDEVS)) {
514*7c478bd9Sstevel@tonic-gate 				*found = t_dev;
515*7c478bd9Sstevel@tonic-gate 			}
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate 		} else if ((mode == SCAN_ALL_CDS) && (writers_found == 0) &&
518*7c478bd9Sstevel@tonic-gate 		    (total_devices_found == 1) && found) {
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 			/* We found a CD-ROM or DVD-ROM */
521*7c478bd9Sstevel@tonic-gate 			*found = t_dev;
522*7c478bd9Sstevel@tonic-gate 		}
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 		if ((mode == SCAN_LISTDEVS) && (!defer)) {
525*7c478bd9Sstevel@tonic-gate 			char *sn;
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 			sn = volmgt_symname(sdev);
528*7c478bd9Sstevel@tonic-gate 			if (!header_printed) {
529*7c478bd9Sstevel@tonic-gate 				print_header();
530*7c478bd9Sstevel@tonic-gate 				header_printed = 1;
531*7c478bd9Sstevel@tonic-gate 			}
532*7c478bd9Sstevel@tonic-gate 			/* show vendor, model, firmware rev and device type */
533*7c478bd9Sstevel@tonic-gate 			(void) printf(" %-21.21s| %.8s %.16s %.4s | %s%s\n",
534*7c478bd9Sstevel@tonic-gate 			    sn ? sn : sdev, &t_dev->d_inq[8],
535*7c478bd9Sstevel@tonic-gate 			    &t_dev->d_inq[16], &t_dev->d_inq[32],
536*7c478bd9Sstevel@tonic-gate 			    gettext("CD Reader"),
537*7c478bd9Sstevel@tonic-gate 			    is_writer ? gettext("/Writer") : "");
538*7c478bd9Sstevel@tonic-gate 			if (sn)
539*7c478bd9Sstevel@tonic-gate 				free(sn);
540*7c478bd9Sstevel@tonic-gate 		}
541*7c478bd9Sstevel@tonic-gate 		if ((found != NULL) && ((*found) != t_dev))
542*7c478bd9Sstevel@tonic-gate 			fini_device(t_dev);
543*7c478bd9Sstevel@tonic-gate 	}
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 	(void) closedir(dir);
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate 	/*
549*7c478bd9Sstevel@tonic-gate 	 * If volume manager is running we'll do a retry in case the
550*7c478bd9Sstevel@tonic-gate 	 * user has just inserted media, This should allow vold
551*7c478bd9Sstevel@tonic-gate 	 * enough time to create the device links. If we cannot
552*7c478bd9Sstevel@tonic-gate 	 * find any devices, or we find any new devices we'll retry
553*7c478bd9Sstevel@tonic-gate 	 * again up to MAX_RETRIES_FOR_SCANNING
554*7c478bd9Sstevel@tonic-gate 	 */
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 	retry++;
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 	if ((retry <= MAX_RETRIES_FOR_SCANNING) && vol_running) {
559*7c478bd9Sstevel@tonic-gate 		if (defer || ((mode != SCAN_LISTDEVS) &&
560*7c478bd9Sstevel@tonic-gate 		    (writers_found == 0))) {
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 			if ((total_devices_found == 0) ||
563*7c478bd9Sstevel@tonic-gate 			    (total_devices_found !=
564*7c478bd9Sstevel@tonic-gate 			    total_devices_found_last_time)) {
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 				/* before we rescan remove the device found */
567*7c478bd9Sstevel@tonic-gate 				if (total_devices_found != 0 && t_dev) {
568*7c478bd9Sstevel@tonic-gate 					fini_device(t_dev);
569*7c478bd9Sstevel@tonic-gate 				}
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate 				total_devices_found_last_time =
572*7c478bd9Sstevel@tonic-gate 				    total_devices_found;
573*7c478bd9Sstevel@tonic-gate 					(void) sleep(2);
574*7c478bd9Sstevel@tonic-gate 					goto try_again;
575*7c478bd9Sstevel@tonic-gate 			} else {
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 				/* Do the printing this time */
578*7c478bd9Sstevel@tonic-gate 				defer = 0;
579*7c478bd9Sstevel@tonic-gate 				goto try_again;
580*7c478bd9Sstevel@tonic-gate 
581*7c478bd9Sstevel@tonic-gate 			}
582*7c478bd9Sstevel@tonic-gate 
583*7c478bd9Sstevel@tonic-gate 		}
584*7c478bd9Sstevel@tonic-gate 	}
585*7c478bd9Sstevel@tonic-gate 	if ((mode & SCAN_WRITERS) || writers_found)
586*7c478bd9Sstevel@tonic-gate 		return (writers_found);
587*7c478bd9Sstevel@tonic-gate 	else
588*7c478bd9Sstevel@tonic-gate 		return (total_devices_found);
589*7c478bd9Sstevel@tonic-gate }
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate /*
592*7c478bd9Sstevel@tonic-gate  * Check device for various conditions/capabilities
593*7c478bd9Sstevel@tonic-gate  * If EXIT_IF_CHECK_FAILED set in cond then it will also exit after
594*7c478bd9Sstevel@tonic-gate  * printing a message.
595*7c478bd9Sstevel@tonic-gate  */
596*7c478bd9Sstevel@tonic-gate int
597*7c478bd9Sstevel@tonic-gate check_device(cd_device *dev, int cond)
598*7c478bd9Sstevel@tonic-gate {
599*7c478bd9Sstevel@tonic-gate 	uchar_t *disc_info, disc_status = 0, erasable = 0;
600*7c478bd9Sstevel@tonic-gate 	uchar_t page_code[4];
601*7c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_TYPE_NOT_CDROM) &&
604*7c478bd9Sstevel@tonic-gate 	    ((dev->d_inq[0] & 0x1f) != 5)) {
605*7c478bd9Sstevel@tonic-gate 		errmsg =
606*7c478bd9Sstevel@tonic-gate 		    gettext("Specified device does not appear to be a CDROM");
607*7c478bd9Sstevel@tonic-gate 	}
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_READY) &&
610*7c478bd9Sstevel@tonic-gate 	    !test_unit_ready(dev->d_fd)) {
611*7c478bd9Sstevel@tonic-gate 		errmsg = gettext("Device not ready");
612*7c478bd9Sstevel@tonic-gate 	}
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 	/* Look at the capabilities page for this information */
615*7c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_WRITABLE)) {
616*7c478bd9Sstevel@tonic-gate 		if (!get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) ||
617*7c478bd9Sstevel@tonic-gate 		    ((page_code[3] & 1) == 0)) {
618*7c478bd9Sstevel@tonic-gate 			errmsg = gettext("Target device is not a CD writer");
619*7c478bd9Sstevel@tonic-gate 		}
620*7c478bd9Sstevel@tonic-gate 	}
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && (cond & CHECK_NO_MEDIA)) {
623*7c478bd9Sstevel@tonic-gate 		if (!test_unit_ready(dev->d_fd) && (uscsi_status == 2) &&
624*7c478bd9Sstevel@tonic-gate 		    ((RQBUFLEN - rqresid) >= 14) &&
625*7c478bd9Sstevel@tonic-gate 		    ((SENSE_KEY(rqbuf) & 0x0f) == 2) && (ASC(rqbuf) == 0x3A) &&
626*7c478bd9Sstevel@tonic-gate 		    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
627*7c478bd9Sstevel@tonic-gate 		    (ASCQ(rqbuf) == 2))) {
628*7c478bd9Sstevel@tonic-gate 			/* medium not present */
629*7c478bd9Sstevel@tonic-gate 			errmsg = gettext("No media in device");
630*7c478bd9Sstevel@tonic-gate 		}
631*7c478bd9Sstevel@tonic-gate 	}
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	/* Issue READ DISC INFORMATION mmc command */
636*7c478bd9Sstevel@tonic-gate 	if ((errmsg == NULL) && ((cond & CHECK_MEDIA_IS_NOT_BLANK) ||
637*7c478bd9Sstevel@tonic-gate 	    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) ||
638*7c478bd9Sstevel@tonic-gate 	    (cond & CHECK_MEDIA_IS_NOT_ERASABLE))) {
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 		disc_info = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
641*7c478bd9Sstevel@tonic-gate 		if (!read_disc_info(dev->d_fd, disc_info)) {
642*7c478bd9Sstevel@tonic-gate 			errmsg = gettext("Cannot obtain disc information");
643*7c478bd9Sstevel@tonic-gate 		} else {
644*7c478bd9Sstevel@tonic-gate 			disc_status = disc_info[2] & 0x03;
645*7c478bd9Sstevel@tonic-gate 			erasable = disc_info[2] & 0x10;
646*7c478bd9Sstevel@tonic-gate 		}
647*7c478bd9Sstevel@tonic-gate 		free(disc_info);
648*7c478bd9Sstevel@tonic-gate 		if (errmsg == NULL) {
649*7c478bd9Sstevel@tonic-gate 			if (!erasable && (cond & CHECK_MEDIA_IS_NOT_ERASABLE))
650*7c478bd9Sstevel@tonic-gate 				errmsg = gettext(
651*7c478bd9Sstevel@tonic-gate 				    "Media in the device is not erasable");
652*7c478bd9Sstevel@tonic-gate 			else if ((disc_status != 0) &&
653*7c478bd9Sstevel@tonic-gate 			    (cond & CHECK_MEDIA_IS_NOT_BLANK))
654*7c478bd9Sstevel@tonic-gate 				errmsg = gettext(
655*7c478bd9Sstevel@tonic-gate 				    "Media in the device is not blank");
656*7c478bd9Sstevel@tonic-gate 			else if ((disc_status == 2) &&
657*7c478bd9Sstevel@tonic-gate 			    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) &&
658*7c478bd9Sstevel@tonic-gate 			    ((device_type != DVD_PLUS_W) &&
659*7c478bd9Sstevel@tonic-gate 			    (device_type != DVD_PLUS)))
660*7c478bd9Sstevel@tonic-gate 				errmsg = gettext(
661*7c478bd9Sstevel@tonic-gate 				    "Media in the device is not writable");
662*7c478bd9Sstevel@tonic-gate 		}
663*7c478bd9Sstevel@tonic-gate 	}
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate 	if (errmsg) {
666*7c478bd9Sstevel@tonic-gate 		if (cond & EXIT_IF_CHECK_FAILED) {
667*7c478bd9Sstevel@tonic-gate 			err_msg("%s.\n", errmsg);
668*7c478bd9Sstevel@tonic-gate 			exit(1);
669*7c478bd9Sstevel@tonic-gate 		}
670*7c478bd9Sstevel@tonic-gate 		return (1);
671*7c478bd9Sstevel@tonic-gate 	}
672*7c478bd9Sstevel@tonic-gate 	return (0);
673*7c478bd9Sstevel@tonic-gate }
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate /*
676*7c478bd9Sstevel@tonic-gate  * Generic routine for writing whatever the next track is and taking
677*7c478bd9Sstevel@tonic-gate  * care of the progress bar. Mode tells the track type (audio or data).
678*7c478bd9Sstevel@tonic-gate  * Data from track is taken from the byte stream h
679*7c478bd9Sstevel@tonic-gate  */
680*7c478bd9Sstevel@tonic-gate void
681*7c478bd9Sstevel@tonic-gate write_next_track(int mode, bstreamhandle h)
682*7c478bd9Sstevel@tonic-gate {
683*7c478bd9Sstevel@tonic-gate 	struct track_info *ti;
684*7c478bd9Sstevel@tonic-gate 	struct trackio_error *te;
685*7c478bd9Sstevel@tonic-gate 	off_t size;
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate 	ti = (struct track_info *)my_zalloc(TRACK_INFO_SIZE);
688*7c478bd9Sstevel@tonic-gate 	if ((build_track_info(target, -1, ti) == 0) ||
689*7c478bd9Sstevel@tonic-gate 	    ((ti->ti_flags & TI_NWA_VALID) == 0)) {
690*7c478bd9Sstevel@tonic-gate 		if ((device_type == DVD_PLUS) || (device_type ==
691*7c478bd9Sstevel@tonic-gate 		    DVD_PLUS_W)) {
692*7c478bd9Sstevel@tonic-gate 			ti->ti_flags |= TI_NWA_VALID;
693*7c478bd9Sstevel@tonic-gate 		} else {
694*7c478bd9Sstevel@tonic-gate 			err_msg(gettext(
695*7c478bd9Sstevel@tonic-gate 			    "Cannot get writable address for the media.\n"));
696*7c478bd9Sstevel@tonic-gate 			exit(1);
697*7c478bd9Sstevel@tonic-gate 		}
698*7c478bd9Sstevel@tonic-gate 	}
699*7c478bd9Sstevel@tonic-gate 	if (ti->ti_nwa != ti->ti_start_address) {
700*7c478bd9Sstevel@tonic-gate 		err_msg(gettext(
701*7c478bd9Sstevel@tonic-gate 		    "Media state is not suitable for this write mode.\n"));
702*7c478bd9Sstevel@tonic-gate 		exit(1);
703*7c478bd9Sstevel@tonic-gate 	}
704*7c478bd9Sstevel@tonic-gate 	if (mode == TRACK_MODE_DATA) {
705*7c478bd9Sstevel@tonic-gate 		if (!(ti->ti_track_mode & 4)) {
706*7c478bd9Sstevel@tonic-gate 			/* Write track depends upon this bit */
707*7c478bd9Sstevel@tonic-gate 			ti->ti_track_mode |= TRACK_MODE_DATA;
708*7c478bd9Sstevel@tonic-gate 		}
709*7c478bd9Sstevel@tonic-gate 	}
710*7c478bd9Sstevel@tonic-gate 	size = 0;
711*7c478bd9Sstevel@tonic-gate 	h->bstr_size(h, &size);
712*7c478bd9Sstevel@tonic-gate 	h->bstr_rewind(h);
713*7c478bd9Sstevel@tonic-gate 	te = (struct trackio_error *)my_zalloc(sizeof (*te));
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Writing track %d..."), (int)ti->ti_track_no);
716*7c478bd9Sstevel@tonic-gate 	init_progress();
717*7c478bd9Sstevel@tonic-gate 	if (!write_track(target, ti, h, progress, (void *)size, te)) {
718*7c478bd9Sstevel@tonic-gate 		if (te->err_type == TRACKIO_ERR_USER_ABORT) {
719*7c478bd9Sstevel@tonic-gate 			(void) str_print(gettext("Aborted.\n"), progress_pos);
720*7c478bd9Sstevel@tonic-gate 		} else {
721*7c478bd9Sstevel@tonic-gate 			if (device_type != DVD_PLUS_W) {
722*7c478bd9Sstevel@tonic-gate 			/* l10n_NOTE : 'failed' as in Writing Track...failed  */
723*7c478bd9Sstevel@tonic-gate 				(void) str_print(gettext("failed.\n"),
724*7c478bd9Sstevel@tonic-gate 				    progress_pos);
725*7c478bd9Sstevel@tonic-gate 			}
726*7c478bd9Sstevel@tonic-gate 		}
727*7c478bd9Sstevel@tonic-gate 	}
728*7c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Writing track 1...done"  */
729*7c478bd9Sstevel@tonic-gate 	(void) str_print(gettext("done.\n"), progress_pos);
730*7c478bd9Sstevel@tonic-gate 	free(ti);
731*7c478bd9Sstevel@tonic-gate 	free(te);
732*7c478bd9Sstevel@tonic-gate }
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate void
735*7c478bd9Sstevel@tonic-gate list(void)
736*7c478bd9Sstevel@tonic-gate {
737*7c478bd9Sstevel@tonic-gate 	if (scan_for_cd_device(SCAN_LISTDEVS, NULL) == 0) {
738*7c478bd9Sstevel@tonic-gate 		if (vol_running) {
739*7c478bd9Sstevel@tonic-gate 			err_msg(gettext(
740*7c478bd9Sstevel@tonic-gate 			    "No CD writers found or no media in the drive.\n"));
741*7c478bd9Sstevel@tonic-gate 		} else {
742*7c478bd9Sstevel@tonic-gate 			if (cur_uid != 0) {
743*7c478bd9Sstevel@tonic-gate 				err_msg(gettext(
744*7c478bd9Sstevel@tonic-gate 				    "Volume manager is not running.\n"));
745*7c478bd9Sstevel@tonic-gate 				err_msg(gettext(
746*7c478bd9Sstevel@tonic-gate "Please start volume manager or run cdrw as root to access all devices.\n"));
747*7c478bd9Sstevel@tonic-gate 			} else {
748*7c478bd9Sstevel@tonic-gate 				err_msg(gettext("No CD writers found.\n"));
749*7c478bd9Sstevel@tonic-gate 			}
750*7c478bd9Sstevel@tonic-gate 		}
751*7c478bd9Sstevel@tonic-gate 	}
752*7c478bd9Sstevel@tonic-gate 	exit(0);
753*7c478bd9Sstevel@tonic-gate }
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate void
756*7c478bd9Sstevel@tonic-gate get_media_type(int fd)
757*7c478bd9Sstevel@tonic-gate {
758*7c478bd9Sstevel@tonic-gate 	uchar_t cap[DVD_CONFIG_SIZE];
759*7c478bd9Sstevel@tonic-gate 	uint_t i;
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	(void) memset(cap, 0, DVD_CONFIG_SIZE);
763*7c478bd9Sstevel@tonic-gate 	if (get_configuration(fd, 0, DVD_CONFIG_SIZE, cap)) {
764*7c478bd9Sstevel@tonic-gate 		if (debug && verbose) {
765*7c478bd9Sstevel@tonic-gate 			(void) printf("\nprofile = ");
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 			for (i = 7; i < 0x20; i += 4)
768*7c478bd9Sstevel@tonic-gate 				(void) printf(" 0x%x", cap[i]);
769*7c478bd9Sstevel@tonic-gate 			(void) printf("\n");
770*7c478bd9Sstevel@tonic-gate 		}
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate 		switch (cap[7]) {
773*7c478bd9Sstevel@tonic-gate 			case 0x8: /* CD-ROM */
774*7c478bd9Sstevel@tonic-gate 				if (debug)
775*7c478bd9Sstevel@tonic-gate 					(void) printf("CD-ROM found\n");
776*7c478bd9Sstevel@tonic-gate 				/*
777*7c478bd9Sstevel@tonic-gate 				 * To avoid regression issues, treat as
778*7c478bd9Sstevel@tonic-gate 				 * A cdrw, we will check the writable
779*7c478bd9Sstevel@tonic-gate 				 * mode page to see if the media is
780*7c478bd9Sstevel@tonic-gate 				 * actually writable.
781*7c478bd9Sstevel@tonic-gate 				 */
782*7c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
783*7c478bd9Sstevel@tonic-gate 				break;
784*7c478bd9Sstevel@tonic-gate 
785*7c478bd9Sstevel@tonic-gate 			case 0x9: /* CD-R */
786*7c478bd9Sstevel@tonic-gate 				if (debug)
787*7c478bd9Sstevel@tonic-gate 					(void) printf("CD-R found\n");
788*7c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
789*7c478bd9Sstevel@tonic-gate 				break;
790*7c478bd9Sstevel@tonic-gate 
791*7c478bd9Sstevel@tonic-gate 			case 0x10: /* DVD-ROM */
792*7c478bd9Sstevel@tonic-gate 				/*
793*7c478bd9Sstevel@tonic-gate 				 * Have seen drives return DVD+RW media
794*7c478bd9Sstevel@tonic-gate 				 * DVD-ROM, so try treating it as a DVD+RW
795*7c478bd9Sstevel@tonic-gate 				 * profile. checking for writable media
796*7c478bd9Sstevel@tonic-gate 				 * is done through mode page 5.
797*7c478bd9Sstevel@tonic-gate 				 */
798*7c478bd9Sstevel@tonic-gate 				if (debug)
799*7c478bd9Sstevel@tonic-gate 					(void) printf("DVD-ROM found\n");
800*7c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS_W;
801*7c478bd9Sstevel@tonic-gate 				break;
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate 			case 0xA: /* CD-RW */
804*7c478bd9Sstevel@tonic-gate 				if (debug)
805*7c478bd9Sstevel@tonic-gate 					(void) printf("CD-RW found\n");
806*7c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
807*7c478bd9Sstevel@tonic-gate 				break;
808*7c478bd9Sstevel@tonic-gate 
809*7c478bd9Sstevel@tonic-gate 			case 0x11: /* DVD-R */
810*7c478bd9Sstevel@tonic-gate 				if (debug)
811*7c478bd9Sstevel@tonic-gate 					(void) printf("DVD-R found\n");
812*7c478bd9Sstevel@tonic-gate 				device_type = DVD_MINUS;
813*7c478bd9Sstevel@tonic-gate 				break;
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate 			case 0x12: /* DVD-RAM */
816*7c478bd9Sstevel@tonic-gate 				if (debug)
817*7c478bd9Sstevel@tonic-gate 					(void) printf("DVD-RAM found\n");
818*7c478bd9Sstevel@tonic-gate 				/* treat as CD-RW, may be a legacy drive */
819*7c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
820*7c478bd9Sstevel@tonic-gate 				break;
821*7c478bd9Sstevel@tonic-gate 
822*7c478bd9Sstevel@tonic-gate 			case 0x13: /* DVD-RW restricted overwrite */
823*7c478bd9Sstevel@tonic-gate 			case 0x14: /* DVD-RW sequencial */
824*7c478bd9Sstevel@tonic-gate 				if (debug)
825*7c478bd9Sstevel@tonic-gate 					(void) printf("DVD-RW found\n");
826*7c478bd9Sstevel@tonic-gate 				device_type = DVD_MINUS;
827*7c478bd9Sstevel@tonic-gate 				break;
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate 			case 0x1A: /* DVD+RW */
830*7c478bd9Sstevel@tonic-gate 				if (debug)
831*7c478bd9Sstevel@tonic-gate 					(void) printf("DVD+RW found\n");
832*7c478bd9Sstevel@tonic-gate 
833*7c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS_W;
834*7c478bd9Sstevel@tonic-gate 				break;
835*7c478bd9Sstevel@tonic-gate 			case 0x1B: /* DVD+R */
836*7c478bd9Sstevel@tonic-gate 				if (debug)
837*7c478bd9Sstevel@tonic-gate 					(void) printf("DVD+R found\n");
838*7c478bd9Sstevel@tonic-gate 				device_type = DVD_PLUS;
839*7c478bd9Sstevel@tonic-gate 				break;
840*7c478bd9Sstevel@tonic-gate 
841*7c478bd9Sstevel@tonic-gate 			default:
842*7c478bd9Sstevel@tonic-gate 				if (debug)
843*7c478bd9Sstevel@tonic-gate 					(void) printf(
844*7c478bd9Sstevel@tonic-gate 					"unknown drive found\n type = 0x%x",
845*7c478bd9Sstevel@tonic-gate 					cap[7]);
846*7c478bd9Sstevel@tonic-gate 				/*
847*7c478bd9Sstevel@tonic-gate 				 * Treat as CD_RW to avoid regression, may
848*7c478bd9Sstevel@tonic-gate 				 * be a legacy drive.
849*7c478bd9Sstevel@tonic-gate 				 */
850*7c478bd9Sstevel@tonic-gate 				device_type = CD_RW;
851*7c478bd9Sstevel@tonic-gate 		}
852*7c478bd9Sstevel@tonic-gate 	}
853*7c478bd9Sstevel@tonic-gate }
854