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