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