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