xref: /titanic_50/usr/src/cmd/cdrw/device.c (revision e8031f0a8ed0e45c6d8847c5e09424e66fd34a4b)
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 2005 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 feature 8 (see GET CONF MMC command) is supported,
210 		 * READ CD will work and will return jitter free audio data.
211 		 * Otherwise look at page code 2A for this info.
212 		 */
213 		if (get_configuration(fd, 0x1E, 8, cap)) {
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 feature 0x0107 is suported then Real-time streaming
225 		 * commands can be used for speed control. Otherwise try
226 		 * SET CD SPEED.
227 		 */
228 		if (get_configuration(fd, 0x0107, 8, cap)) {
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 		/*
251 		 * If DKIOCGMEDIAINFO fails we'll try to get
252 		 * the blocksize from the device itself.
253 		 */
254 
255 		if (debug)
256 			(void) printf("DKIOCGMEDIAINFO failed\n");
257 
258 		if (read_capacity(fd, cap))
259 			dev->d_blksize = read_scsi32(cap + 4);
260 	} else {
261 
262 		dev->d_blksize = mediainfo.dki_lbsize;
263 	}
264 
265 	if (debug) {
266 		uint_t bsize;
267 
268 		(void) printf("blocksize = %d\n", dev->d_blksize);
269 		(void) printf("read_format_capacity = %d \n",
270 		    read_format_capacity(fd, &bsize));
271 	}
272 
273 	/*
274 	 * Some devices will return invalid blocksizes. ie. Toshiba
275 	 * drives will return 2352 when an audio CD is inserted.
276 	 * Older Sun drives will use 512 byte block sizes. All newer
277 	 * drives should have 2k blocksizes.
278 	 */
279 	if (((dev->d_blksize != 512) && (dev->d_blksize != 2048))) {
280 			if (is_old_sun_drive(dev)) {
281 				dev->d_blksize = 512;
282 			} else {
283 				dev->d_blksize = 2048;
284 			}
285 		if (debug)
286 			(void) printf(" switching to %d\n", dev->d_blksize);
287 	}
288 
289 	free(cap);
290 	if (user_supplied) {
291 		dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
292 		(void) strcpy(dev->d_name, user_supplied);
293 	}
294 	TRACE(traceall_msg("Got device %s\n", node));
295 	return (dev);
296 }
297 
298 void
299 fini_device(cd_device *dev)
300 {
301 	free(dev->d_inq);
302 	free(dev->d_node);
303 	(void) close(dev->d_fd);
304 	if (dev->d_name)
305 		free(dev->d_name);
306 	free(dev);
307 }
308 
309 static int
310 vol_name_to_dev_node(char *vname, char *found)
311 {
312 	struct stat statbuf;
313 	char *p1;
314 	int i;
315 
316 	if (vname == NULL)
317 		return (0);
318 	if (vol_running)
319 		(void) volmgt_check(vname);
320 	p1 = media_findname(vname);
321 	if (p1 == NULL)
322 		return (0);
323 	if (stat(p1, &statbuf) < 0) {
324 		free(p1);
325 		return (0);
326 	}
327 	if (S_ISDIR(statbuf.st_mode)) {
328 		for (i = 0; i < 16; i++) {
329 			(void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
330 			if (access(found, F_OK) >= 0)
331 				break;
332 		}
333 		if (i == 16) {
334 			free(p1);
335 			return (0);
336 		}
337 	} else {
338 		(void) strlcpy(found, p1, PATH_MAX);
339 	}
340 	free(p1);
341 	return (1);
342 }
343 
344 /*
345  * Searches for volume manager's equivalent char device for the
346  * supplied pathname which is of the form of /dev/rdsk/cxtxdxsx
347  */
348 static int
349 vol_lookup(char *supplied, char *found)
350 {
351 	char tmpstr[PATH_MAX], tmpstr1[PATH_MAX], *p;
352 	int i, ret;
353 
354 	(void) strlcpy(tmpstr, supplied, PATH_MAX);
355 	if ((p = volmgt_symname(tmpstr)) == NULL) {
356 		if (strrchr(tmpstr, 's') == NULL)
357 			return (0);
358 		*((char *)(strrchr(tmpstr, 's') + 1)) = 0;
359 		for (i = 0; i < 16; i++) {
360 			(void) snprintf(tmpstr1, PATH_MAX, "%s%d", tmpstr, i);
361 			if ((p = volmgt_symname(tmpstr1)) != NULL)
362 				break;
363 		}
364 		if (p == NULL)
365 			return (0);
366 	}
367 	ret = vol_name_to_dev_node(p, found);
368 	free(p);
369 	return (ret);
370 }
371 
372 /*
373  * Builds an open()able device path from a user supplied node which can be
374  * of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
375  * cdrom[n]
376  * returns the path found in 'found' and returns 1. Otherwise returns 0.
377  */
378 int
379 lookup_device(char *supplied, char *found)
380 {
381 	struct stat statbuf;
382 	int fd;
383 	char tmpstr[PATH_MAX];
384 
385 	/* If everything is fine and proper, no need to analyze */
386 	if ((stat(supplied, &statbuf) == 0) && S_ISCHR(statbuf.st_mode) &&
387 	    ((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
388 		(void) close(fd);
389 		(void) strlcpy(found, supplied, PATH_MAX);
390 		return (1);
391 	}
392 	if (strncmp(supplied, "/dev/rdsk/", 10) == 0)
393 		return (vol_lookup(supplied, found));
394 	if (strncmp(supplied, "/dev/dsk/", 9) == 0) {
395 		(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
396 		    (char *)strrchr(supplied, '/'));
397 
398 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
399 			(void) close(fd);
400 			(void) strlcpy(found, supplied, PATH_MAX);
401 			return (1);
402 		}
403 		if ((access(tmpstr, F_OK) == 0) && vol_running)
404 			return (vol_lookup(tmpstr, found));
405 		else
406 			return (0);
407 	}
408 	if ((strncmp(supplied, "cdrom", 5) != 0) &&
409 	    (strlen(supplied) < 32)) {
410 		(void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
411 		    supplied);
412 		if (access(tmpstr, F_OK) < 0) {
413 			(void) strcat(tmpstr, "s2");
414 		}
415 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
416 			(void) close(fd);
417 			(void) strlcpy(found, tmpstr, PATH_MAX);
418 			return (1);
419 		}
420 		if ((access(tmpstr, F_OK) == 0) && vol_running)
421 			return (vol_lookup(tmpstr, found));
422 	}
423 	return (vol_name_to_dev_node(supplied, found));
424 }
425 
426 /*
427  * Opens the device node name passed and returns 1 (true) if the
428  * device is a CD.
429  */
430 
431 static int
432 is_cd(char *node)
433 {
434 	int fd;
435 	struct dk_cinfo cinfo;
436 	int ret = 1;
437 
438 	fd = open(node, O_RDONLY|O_NDELAY);
439 	if (fd < 0) {
440 		ret = 0;
441 	} else if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
442 		ret = 0;
443 	} else if (cinfo.dki_ctype != DKC_CDROM) {
444 		ret = 0;
445 	}
446 
447 	if (fd >= 0) {
448 		(void) close(fd);
449 	}
450 	return (ret);
451 }
452 
453 static void
454 print_header(void)
455 {
456 	/* l10n_NOTE : Column spacing should be kept same */
457 	(void) printf(gettext("    Node	           Connected Device"));
458 	/* l10n_NOTE : Column spacing should be kept same */
459 	(void) printf(gettext("	           Device type\n"));
460 	(void) printf(
461 	    "----------------------+--------------------------------");
462 	(void) printf("+-----------------\n");
463 }
464 
465 /*
466  * returns the number of writers or CD/DVD-roms found and the path of
467  * the first device found depending on the mode argument.
468  * possible mode values are:
469  * SCAN_ALL_CDS 	Scan all CD/DVD devices. Return first CD-RW found.
470  * SCAN_WRITERS		Scan all CD-RW devices. Return first one found.
471  * SCAN_LISTDEVS	List all devices found.
472  */
473 int
474 scan_for_cd_device(int mode, cd_device **found)
475 {
476 	DIR *dir;
477 	struct dirent *dirent;
478 	char sdev[PATH_MAX], dev[PATH_MAX];
479 	cd_device *t_dev;
480 	int writers_found = 0;
481 	int header_printed = 0;
482 	int is_writer;
483 	int retry = 0;
484 	int total_devices_found;
485 	int total_devices_found_last_time = 0;
486 	int defer = 0;
487 
488 #define	MAX_RETRIES_FOR_SCANNING 5
489 
490 	TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode));
491 
492 	if (mode) {
493 		if (vol_running)
494 			defer = 1;
495 		(void) printf(gettext("Looking for CD devices...\n"));
496 	}
497 try_again:
498 
499 	dir = opendir("/dev/rdsk");
500 	if (dir == NULL)
501 		return (0);
502 
503 	writers_found = 0;
504 	total_devices_found = 0;
505 	while ((dirent = readdir(dir)) != NULL) {
506 		if (dirent->d_name[0] == '.')
507 			continue;
508 		(void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
509 		    dirent->d_name);
510 		if (strcmp("s2", (char *)strrchr(sdev, 's')) != 0)
511 			continue;
512 		if (!lookup_device(sdev, dev))
513 			continue;
514 		if (!is_cd(dev))
515 			continue;
516 		if ((t_dev = get_device(NULL, dev)) == NULL) {
517 			continue;
518 		}
519 		total_devices_found++;
520 
521 		is_writer = !(check_device(t_dev, CHECK_DEVICE_NOT_WRITABLE));
522 
523 		if (is_writer) {
524 			writers_found++;
525 
526 			if ((writers_found == 1) && (mode != SCAN_LISTDEVS)) {
527 				*found = t_dev;
528 			}
529 
530 		} else if ((mode == SCAN_ALL_CDS) && (writers_found == 0) &&
531 		    (total_devices_found == 1) && found) {
532 
533 			/* We found a CD-ROM or DVD-ROM */
534 			*found = t_dev;
535 		}
536 
537 		if ((mode == SCAN_LISTDEVS) && (!defer)) {
538 			char *sn;
539 
540 			sn = volmgt_symname(sdev);
541 			if (!header_printed) {
542 				print_header();
543 				header_printed = 1;
544 			}
545 			/* show vendor, model, firmware rev and device type */
546 			(void) printf(" %-21.21s| %.8s %.16s %.4s | %s%s\n",
547 			    sn ? sn : sdev, &t_dev->d_inq[8],
548 			    &t_dev->d_inq[16], &t_dev->d_inq[32],
549 			    gettext("CD Reader"),
550 			    is_writer ? gettext("/Writer") : "");
551 			if (sn)
552 				free(sn);
553 		}
554 		if ((found != NULL) && ((*found) != t_dev))
555 			fini_device(t_dev);
556 	}
557 
558 	(void) closedir(dir);
559 
560 
561 	/*
562 	 * If volume manager is running we'll do a retry in case the
563 	 * user has just inserted media, This should allow vold
564 	 * enough time to create the device links. If we cannot
565 	 * find any devices, or we find any new devices we'll retry
566 	 * again up to MAX_RETRIES_FOR_SCANNING
567 	 */
568 
569 	retry++;
570 
571 	if ((retry <= MAX_RETRIES_FOR_SCANNING) && vol_running) {
572 		if (defer || ((mode != SCAN_LISTDEVS) &&
573 		    (writers_found == 0))) {
574 
575 			if ((total_devices_found == 0) ||
576 			    (total_devices_found !=
577 			    total_devices_found_last_time)) {
578 
579 				/* before we rescan remove the device found */
580 				if (total_devices_found != 0 && t_dev) {
581 					fini_device(t_dev);
582 				}
583 
584 				total_devices_found_last_time =
585 				    total_devices_found;
586 					(void) sleep(2);
587 					goto try_again;
588 			} else {
589 
590 				/* Do the printing this time */
591 				defer = 0;
592 				goto try_again;
593 
594 			}
595 
596 		}
597 	}
598 	if ((mode & SCAN_WRITERS) || writers_found)
599 		return (writers_found);
600 	else
601 		return (total_devices_found);
602 }
603 
604 /*
605  * Check device for various conditions/capabilities
606  * If EXIT_IF_CHECK_FAILED set in cond then it will also exit after
607  * printing a message.
608  */
609 int
610 check_device(cd_device *dev, int cond)
611 {
612 	uchar_t *disc_info, disc_status = 0, erasable = 0;
613 	uchar_t page_code[4];
614 	char *errmsg = NULL;
615 
616 	if ((errmsg == NULL) && (cond & CHECK_TYPE_NOT_CDROM) &&
617 	    ((dev->d_inq[0] & 0x1f) != 5)) {
618 		errmsg =
619 		    gettext("Specified device does not appear to be a CDROM");
620 	}
621 
622 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_READY) &&
623 	    !test_unit_ready(dev->d_fd)) {
624 		errmsg = gettext("Device not ready");
625 	}
626 
627 	/* Look at the capabilities page for this information */
628 	if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_WRITABLE)) {
629 		if (!get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) ||
630 		    ((page_code[3] & 1) == 0)) {
631 			errmsg = gettext("Target device is not a CD writer");
632 		}
633 	}
634 
635 	if ((errmsg == NULL) && (cond & CHECK_NO_MEDIA)) {
636 		if (!test_unit_ready(dev->d_fd) && (uscsi_status == 2) &&
637 		    ((RQBUFLEN - rqresid) >= 14) &&
638 		    ((SENSE_KEY(rqbuf) & 0x0f) == 2) && (ASC(rqbuf) == 0x3A) &&
639 		    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
640 		    (ASCQ(rqbuf) == 2))) {
641 			/* medium not present */
642 			errmsg = gettext("No media in device");
643 		}
644 	}
645 
646 
647 
648 	/* Issue READ DISC INFORMATION mmc command */
649 	if ((errmsg == NULL) && ((cond & CHECK_MEDIA_IS_NOT_BLANK) ||
650 	    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) ||
651 	    (cond & CHECK_MEDIA_IS_NOT_ERASABLE))) {
652 
653 		disc_info = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
654 		if (!read_disc_info(dev->d_fd, disc_info)) {
655 			errmsg = gettext("Cannot obtain disc information");
656 		} else {
657 			disc_status = disc_info[2] & 0x03;
658 			erasable = disc_info[2] & 0x10;
659 		}
660 		free(disc_info);
661 		if (errmsg == NULL) {
662 			if (!erasable && (cond & CHECK_MEDIA_IS_NOT_ERASABLE))
663 				errmsg = gettext(
664 				    "Media in the device is not erasable");
665 			else if ((disc_status != 0) &&
666 			    (cond & CHECK_MEDIA_IS_NOT_BLANK))
667 				errmsg = gettext(
668 				    "Media in the device is not blank");
669 			else if ((disc_status == 2) &&
670 			    (cond & CHECK_MEDIA_IS_NOT_WRITABLE) &&
671 			    ((device_type != DVD_PLUS_W) &&
672 			    (device_type != DVD_PLUS)))
673 				errmsg = gettext(
674 				    "Media in the device is not writable");
675 		}
676 	}
677 
678 	if (errmsg) {
679 		if (cond & EXIT_IF_CHECK_FAILED) {
680 			err_msg("%s.\n", errmsg);
681 			exit(1);
682 		}
683 		return (1);
684 	}
685 	return (0);
686 }
687 
688 /*
689  * Generic routine for writing whatever the next track is and taking
690  * care of the progress bar. Mode tells the track type (audio or data).
691  * Data from track is taken from the byte stream h
692  */
693 void
694 write_next_track(int mode, bstreamhandle h)
695 {
696 	struct track_info *ti;
697 	struct trackio_error *te;
698 	off_t size;
699 
700 	ti = (struct track_info *)my_zalloc(sizeof (*ti));
701 	if ((build_track_info(target, -1, ti) == 0) ||
702 	    ((ti->ti_flags & TI_NWA_VALID) == 0)) {
703 		if ((device_type == DVD_PLUS) || (device_type ==
704 		    DVD_PLUS_W)) {
705 			ti->ti_flags |= TI_NWA_VALID;
706 		} else {
707 			err_msg(gettext(
708 			    "Cannot get writable address for the media.\n"));
709 			exit(1);
710 		}
711 	}
712 	if (ti->ti_nwa != ti->ti_start_address) {
713 		err_msg(gettext(
714 		    "Media state is not suitable for this write mode.\n"));
715 		exit(1);
716 	}
717 	if (mode == TRACK_MODE_DATA) {
718 		if (!(ti->ti_track_mode & 4)) {
719 			/* Write track depends upon this bit */
720 			ti->ti_track_mode |= TRACK_MODE_DATA;
721 		}
722 	}
723 	size = 0;
724 	h->bstr_size(h, &size);
725 	h->bstr_rewind(h);
726 	te = (struct trackio_error *)my_zalloc(sizeof (*te));
727 
728 	print_n_flush(gettext("Writing track %d..."), (int)ti->ti_track_no);
729 	init_progress();
730 	if (!write_track(target, ti, h, progress, size, te)) {
731 		if (te->err_type == TRACKIO_ERR_USER_ABORT) {
732 			(void) str_print(gettext("Aborted.\n"), progress_pos);
733 		} else {
734 			if (device_type != DVD_PLUS_W) {
735 			/* l10n_NOTE : 'failed' as in Writing Track...failed  */
736 				(void) str_print(gettext("failed.\n"),
737 				    progress_pos);
738 			}
739 		}
740 	}
741 	/* l10n_NOTE : 'done' as in "Writing track 1...done"  */
742 	(void) str_print(gettext("done.\n"), progress_pos);
743 	free(ti);
744 	free(te);
745 }
746 
747 void
748 list(void)
749 {
750 	if (scan_for_cd_device(SCAN_LISTDEVS, NULL) == 0) {
751 		if (vol_running) {
752 			err_msg(gettext(
753 			    "No CD writers found or no media in the drive.\n"));
754 		} else {
755 			if (cur_uid != 0) {
756 				err_msg(gettext(
757 				    "Volume manager is not running.\n"));
758 				err_msg(gettext(
759 "Please start volume manager or run cdrw as root to access all devices.\n"));
760 			} else {
761 				err_msg(gettext("No CD writers found.\n"));
762 			}
763 		}
764 	}
765 	exit(0);
766 }
767 
768 void
769 get_media_type(int fd)
770 {
771 	uchar_t cap[DVD_CONFIG_SIZE];
772 	uint_t i;
773 
774 
775 	(void) memset(cap, 0, DVD_CONFIG_SIZE);
776 	if (get_configuration(fd, 0, DVD_CONFIG_SIZE, cap)) {
777 		if (debug && verbose) {
778 			(void) printf("\nprofile = ");
779 
780 			for (i = 7; i < 0x20; i += 4)
781 				(void) printf(" 0x%x", cap[i]);
782 			(void) printf("\n");
783 		}
784 
785 		switch (cap[7]) {
786 			case 0x8: /* CD-ROM */
787 				if (debug)
788 					(void) printf("CD-ROM found\n");
789 				/*
790 				 * To avoid regression issues, treat as
791 				 * A cdrw, we will check the writable
792 				 * mode page to see if the media is
793 				 * actually writable.
794 				 */
795 				device_type = CD_RW;
796 				break;
797 
798 			case 0x9: /* CD-R */
799 				if (debug)
800 					(void) printf("CD-R found\n");
801 				device_type = CD_RW;
802 				break;
803 
804 			case 0x10: /* DVD-ROM */
805 				/*
806 				 * Have seen drives return DVD+RW media
807 				 * DVD-ROM, so try treating it as a DVD+RW
808 				 * profile. checking for writable media
809 				 * is done through mode page 5.
810 				 */
811 				if (debug)
812 					(void) printf("DVD-ROM found\n");
813 				device_type = DVD_PLUS_W;
814 				break;
815 
816 			case 0xA: /* CD-RW */
817 				if (debug)
818 					(void) printf("CD-RW found\n");
819 				device_type = CD_RW;
820 				break;
821 
822 			case 0x11: /* DVD-R */
823 				if (debug)
824 					(void) printf("DVD-R found\n");
825 				device_type = DVD_MINUS;
826 				break;
827 
828 			case 0x12: /* DVD-RAM */
829 				if (debug)
830 					(void) printf("DVD-RAM found\n");
831 				/* treat as CD-RW, may be a legacy drive */
832 				device_type = CD_RW;
833 				break;
834 
835 			case 0x13: /* DVD-RW restricted overwrite */
836 			case 0x14: /* DVD-RW sequencial */
837 				if (debug)
838 					(void) printf("DVD-RW found\n");
839 				device_type = DVD_MINUS;
840 				break;
841 
842 			case 0x1A: /* DVD+RW */
843 				if (debug)
844 					(void) printf("DVD+RW found\n");
845 
846 				device_type = DVD_PLUS_W;
847 				break;
848 			case 0x1B: /* DVD+R */
849 				if (debug)
850 					(void) printf("DVD+R found\n");
851 				device_type = DVD_PLUS;
852 				break;
853 
854 			default:
855 				if (debug)
856 					(void) printf(
857 					"unknown drive found\n type = 0x%x",
858 					cap[7]);
859 				/*
860 				 * Treat as CD_RW to avoid regression, may
861 				 * be a legacy drive.
862 				 */
863 				device_type = CD_RW;
864 		}
865 	}
866 }
867 
868 /* Translate a transfer rate (eg, KB/s) into a Speed (eg, "2X") */
869 uint_t
870 cdrw_bandwidth_to_x(uint_t rate)
871 {
872 	switch (device_type) {
873 	case DVD_PLUS_W:
874 	case DVD_MINUS:
875 	case DVD_PLUS:
876 		return (DVD_RATE_TO_X(rate));
877 
878 	default:
879 	case CD_RW:
880 		return (CD_RATE_TO_X(rate));
881 	}
882 }
883 
884 /* Translate a Speed (eg, "2X") into a transfer rate (eg, KB/s) */
885 uint_t
886 cdrw_x_to_bandwidth(uint_t x)
887 {
888 	switch (device_type) {
889 	case DVD_PLUS_W:
890 	case DVD_MINUS:
891 	case DVD_PLUS:
892 		return (DVD_X_TO_RATE(x));
893 
894 	default:
895 	case CD_RW:
896 		return (CD_X_TO_RATE(x));
897 	}
898 }
899