add UNLEASHED_OBJ to unleashed.mk
[unleashed/tickless.git] / usr / src / cmd / cdrw / device.c
blob0e194463f7330901f237ef38380bd6604d1e07f9
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <sys/dkio.h>
31 #include <unistd.h>
32 #include <dirent.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <libintl.h>
36 #include <limits.h>
37 #include <dbus/dbus.h>
38 #include <hal/libhal.h>
40 #include "transport.h"
41 #include "mmc.h"
42 #include "device.h"
43 #include "util.h"
44 #include "msgs.h"
45 #include "misc_scsi.h"
46 #include "toshiba.h"
47 #include "main.h"
50 * Old sun drives have a vendor specific mode page for setting/getting speed.
51 * Also they use a different method for extracting audio.
52 * We have the device inquiry strings at this time. This is used to enable
53 * us to use older sun drives to extract audio.
55 static int
56 is_old_sun_drive(cd_device *dev)
59 * If we have a SONY CDU 561, CDU 8012, or TOSHIBA model with XMa we
60 * need to handle these drives a bit differently.
62 if (strncmp("SONY", (const char *)&dev->d_inq[8], 4) == 0) {
63 if (strncmp("CDU 561", (const char *)&dev->d_inq[16], 7) == 0)
64 return (1);
65 if (strncmp("CDU-8012", (const char *)&dev->d_inq[16], 8) == 0)
66 return (1);
69 if ((strncmp("TOSHIBA", (const char *)&dev->d_inq[8], 7) == 0) &&
70 (strncmp("XM", (const char *)&dev->d_inq[16], 2) == 0)) {
72 char product_id[17];
74 /* Changing speed is not allowed for 32X TOSHIBA drives */
75 if (strncmp("SUN32XCD", (const char *)&dev->d_inq[24], 8) == 0)
76 dev->d_cap |= DEV_CAP_SETTING_SPEED_NOT_ALLOWED;
77 (void) strncpy(product_id, (const char *)&dev->d_inq[16], 16);
78 product_id[16] = 0;
79 if (strstr(product_id, "SUN") != NULL)
80 return (1);
82 return (0);
86 * returns a cd_device handle for a node returned by lookup_device()
87 * also takes the user supplied name and stores it inside the node
89 cd_device *
90 get_device(char *user_supplied, char *node)
92 cd_device *dev;
93 int fd;
94 uchar_t *cap;
95 char devnode[PATH_MAX];
96 int size;
97 struct dk_minfo mediainfo;
98 int use_cd_speed = 0;
101 * we need to resolve any link paths to avoid fake files
102 * such as /dev/rdsk/../../export/file.
105 TRACE(traceall_msg("get_device(%s, %s)\n", user_supplied ?
106 user_supplied : "<nil>", node ? node : "<nil>"));
108 size = resolvepath(node, devnode, PATH_MAX);
109 if ((size <= 0) || (size >= (PATH_MAX - 1)))
110 return (NULL);
112 /* resolvepath may not return a null terminated string */
113 devnode[size] = '\0';
116 /* the device node must be in /devices/ or /vol/dev/rdsk */
118 if ((strncmp(devnode, "/devices/", 9) != 0) &&
119 (strncmp(devnode, "/vol/dev/rdsk", 13) != 0))
120 return (NULL);
122 * Since we are currently running with the user euid it is
123 * safe to try to open the file without checking access.
126 fd = open(devnode, O_RDONLY|O_NDELAY);
128 if (fd < 0) {
129 TRACE(traceall_msg("Cannot open %s: %s\n", node,
130 strerror(errno)));
131 return (NULL);
134 dev = (cd_device *)my_zalloc(sizeof (cd_device));
136 dev->d_node = (char *)my_zalloc(strlen(devnode) + 1);
137 (void) strcpy(dev->d_node, devnode);
139 dev->d_fd = fd;
141 dev->d_inq = (uchar_t *)my_zalloc(INQUIRY_DATA_LENGTH);
143 if (!inquiry(fd, dev->d_inq)) {
144 TRACE(traceall_msg("Inquiry failed on device %s\n", node));
145 if (debug) {
146 (void) printf("USCSI ioctl failed %d\n",
147 uscsi_error);
149 free(dev->d_inq);
150 free(dev->d_node);
151 (void) close(dev->d_fd);
152 free(dev);
153 return (NULL);
156 if (debug) {
157 cap = (uchar_t *)my_zalloc(18);
158 (void) printf("Checking device type\n");
159 if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
160 if (cap[2] & 0x10)
161 (void) printf("DVD-R read support\n");
162 if (cap[3] & 0x10)
163 (void) printf("DVD-R write support\n");
164 if (cap[5] & 0x4)
165 (void) printf("R-W supported\n");
166 if (cap[2] & 0x20)
167 (void) printf("DVD-RAM read supported\n");
168 if (cap[3] & 0x20)
169 (void) printf("DVD-RAM write supported\n");
170 } else {
171 (void) printf("Could not read mode page 2A! \n");
173 free(cap);
176 /* Detect if it's a Lite-ON drive with a streaming CD problem */
177 if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
178 (strncmp("LTR-48", (const char *)&dev->d_inq[16], 6) == 0)) {
179 use_cd_speed = 1;
183 * a workaround for the firmware problem in LITE-ON COMBO drives.
184 * streaming for these drives sets it only to max speed regardless
185 * of requested speed. cd_speed_ctrl allow speeds less than max
186 * to be set but not max thus the code below. (x48 is max speed
187 * for these drives).
189 if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
190 (strncmp("COMBO SOHC-4836VS",
191 (const char *)&dev->d_inq[16], 17) == 0))
192 if (requested_speed < 48)
193 use_cd_speed = 1;
195 cap = (uchar_t *)my_zalloc(8);
196 if (is_old_sun_drive(dev)) {
197 dev->d_read_audio = toshiba_read_audio;
198 dev->d_speed_ctrl = toshiba_speed_ctrl;
199 } else {
201 * If the CD Read Feature is supported, READ CD will work
202 * and will return jitter free audio data. Otherwise, look
203 * at Page Code 2A for this information.
205 if (ftr_supported(fd, MMC_FTR_CD_READ) == 1) {
206 dev->d_read_audio = read_audio_through_read_cd;
207 dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
208 } else if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
209 if (cap[5] & 1) {
210 dev->d_read_audio = read_audio_through_read_cd;
211 if (cap[5] & 2)
212 dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
216 * If the Real Time Streaming Feature is supported then
217 * Real-time streaming commands can be used for speed control
218 * (except when we want to use cd_speed_ctrl explicitly which
219 * is specified by setting use_cd_speed to 1).
220 * Otherwise try SET CD SPEED.
222 if ((ftr_supported(fd, MMC_FTR_RT_STREAM) == 1) &&
223 !use_cd_speed) {
224 dev->d_speed_ctrl = rt_streaming_ctrl;
225 if (debug)
226 (void) printf("using rt speed ctrl\n");
227 } else {
228 dev->d_speed_ctrl = cd_speed_ctrl;
229 if (debug)
230 (void) printf("using cd speed ctrl\n");
233 if (dev->d_read_audio != NULL)
234 dev->d_cap |= DEV_CAP_EXTRACT_CDDA;
236 dev->d_blksize = 0;
239 * Find the block size of the device so we can translate
240 * the reads/writes to the device blocksize.
243 if (ioctl(fd, DKIOCGMEDIAINFO, &mediainfo) < 0) {
245 * If DKIOCGMEDIAINFO fails we'll try to get
246 * the blocksize from the device itself.
248 if (debug)
249 (void) printf("DKIOCGMEDIAINFO failed\n");
250 if (read_capacity(fd, cap))
251 dev->d_blksize = read_scsi32(cap + 4);
252 } else {
254 dev->d_blksize = mediainfo.dki_lbsize;
257 if (debug) {
258 uint_t bsize;
260 (void) printf("blocksize = %d\n", dev->d_blksize);
261 (void) printf("read_format_capacity = %d \n",
262 read_format_capacity(fd, &bsize));
266 * Some devices will return invalid blocksizes. ie. Toshiba
267 * drives will return 2352 when an audio CD is inserted.
268 * Older Sun drives will use 512 byte block sizes. All newer
269 * drives should have 2k blocksizes.
271 if (((dev->d_blksize != 512) && (dev->d_blksize != 2048))) {
272 if (is_old_sun_drive(dev)) {
273 dev->d_blksize = 512;
274 } else {
275 dev->d_blksize = 2048;
277 if (debug)
278 (void) printf(" switching to %d\n", dev->d_blksize);
281 free(cap);
282 if (user_supplied) {
283 dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
284 (void) strcpy(dev->d_name, user_supplied);
286 TRACE(traceall_msg("Got device %s\n", node));
287 return (dev);
290 void
291 fini_device(cd_device *dev)
293 free(dev->d_inq);
294 free(dev->d_node);
295 (void) close(dev->d_fd);
296 free(dev->d_name);
297 free(dev);
301 * Given a /dev path resolve that path to a symbolic
302 * name such as cdrom0 if hald is running. If hald is
303 * not running, or does not have a symbolic name for the
304 * the specified /dev path return NULL.
306 static char *
307 hald_symname(char *path)
309 LibHalContext *ctx = NULL;
310 DBusError error;
312 char **udi, *p = NULL;
313 int ndevs = 0, i;
315 /* Make sure hald is running */
316 if (vol_running == 0)
317 return (p);
319 dbus_error_init(&error);
321 if ((ctx = attach_to_hald()) == NULL)
322 return (p);
324 if ((udi = libhal_manager_find_device_string_match(ctx,
325 HAL_RDSK_PROP, path, &ndevs, &error)) == NULL)
326 goto done;
328 /* Look for the node that contains the valid (non-null) symdev */
329 for (i = 0; i < ndevs; i++) {
330 if ((p = libhal_device_get_property_string(ctx, udi[i],
331 HAL_SYMDEV_PROP, NULL)) != NULL)
332 break;
333 else
334 libhal_free_string(p);
337 done:
338 if (udi != NULL)
339 libhal_free_string_array(udi);
340 if (dbus_error_is_set(&error))
341 dbus_error_free(&error);
342 detach_from_hald(ctx, HAL_INITIALIZED);
343 return (p);
347 * Given a name resolve that name to a raw device in the case
348 * that it is a symbolic name or just return what is given if
349 * we are given a /dev path or hald is not running.
351 static char *
352 hald_findname(char *symname)
354 LibHalContext *ctx = NULL;
355 DBusError error;
357 char **udi, *path = NULL;
358 int ndevs = 0, i;
361 * We already have a raw path lets return it in a copied buffer
362 * as our caller assumes that they need to free memory.
364 if (symname[0] == '/') {
365 path = my_zalloc(strlen(symname) + 1);
366 (void) strlcpy(path, symname, (strlen(symname) + 1));
367 return (path);
370 /* Get the raw device from the hal record */
371 if (vol_running != 0) {
372 dbus_error_init(&error);
374 if ((ctx = attach_to_hald()) == NULL)
375 return (path);
377 if ((udi = libhal_manager_find_device_string_match(ctx,
378 HAL_SYMDEV_PROP, symname, &ndevs,
379 &error)) == NULL)
380 goto done;
383 * Loop over the returned UDIs to access the raw
384 * device path.
386 for (i = 0; i < ndevs; i++) {
387 if ((path = libhal_device_get_property_string(ctx,
388 udi[i], HAL_RDSK_PROP, NULL)) != NULL)
389 break;
390 else
391 libhal_free_string(path);
394 done:
395 if (udi != NULL)
396 libhal_free_string_array(udi);
397 if (dbus_error_is_set(&error))
398 dbus_error_free(&error);
399 detach_from_hald(ctx, HAL_INITIALIZED);
400 return (path);
401 } else {
402 return (NULL);
406 static int
407 vol_name_to_dev_node(char *vname, char *found)
409 struct stat statbuf;
410 char *p1;
411 int i;
413 if (vname == NULL)
414 return (0);
416 p1 = hald_findname(vname);
418 if (p1 == NULL)
419 return (0);
420 if (stat(p1, &statbuf) < 0) {
421 libhal_free_string(p1);
422 return (0);
424 if (S_ISDIR(statbuf.st_mode)) {
425 for (i = 0; i < 16; i++) {
426 (void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
427 if (access(found, F_OK) >= 0)
428 break;
430 if (i == 16) {
431 libhal_free_string(p1);
432 return (0);
434 } else {
435 (void) strlcpy(found, p1, PATH_MAX);
437 libhal_free_string(p1);
438 return (1);
442 * Builds an open()able device path from a user supplied node which can be
443 * of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
444 * cdrom[n]
445 * returns the path found in 'found' and returns 1. Otherwise returns 0.
448 lookup_device(char *supplied, char *found)
450 struct stat statbuf;
451 int fd;
452 char tmpstr[PATH_MAX];
454 /* If everything is fine and proper, no need to analyze */
455 if ((stat(supplied, &statbuf) == 0) && S_ISCHR(statbuf.st_mode) &&
456 ((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
457 (void) close(fd);
458 (void) strlcpy(found, supplied, PATH_MAX);
459 return (1);
463 * Hal only allows access to a device when the user is
464 * on the console, therefore if hal is running and we can't
465 * open the /dev/rdsk or /dev/removable-media/rdsk device
466 * file we will return 0 marking this device as not avaiable.
468 if (fd < 0 && ((strncmp(supplied, "/dev/rdsk/", 10) == 0) ||
469 (strncmp(supplied, "/dev/removable-media/rdsk/", 26) == 0)))
470 return (0);
472 if ((strncmp(supplied, "/dev/dsk/", 9) == 0) ||
473 (strncmp(supplied, "/dev/removable-media/dsk/", 25) == 0)) {
474 (void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
475 (char *)strrchr(supplied, '/'));
477 if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
478 (void) close(fd);
479 (void) strlcpy(found, supplied, PATH_MAX);
480 return (1);
483 /* This device can't be opened mark it as unavailable. */
484 return (0);
486 if ((strncmp(supplied, "cdrom", 5) != 0) &&
487 (strlen(supplied) < 32)) {
488 (void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
489 supplied);
490 if (access(tmpstr, F_OK) < 0) {
491 (void) strcat(tmpstr, "s2");
493 if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
494 (void) close(fd);
495 (void) strlcpy(found, tmpstr, PATH_MAX);
496 return (1);
499 /* This device can't be opened mark it as unavailable. */
500 return (0);
502 return (vol_name_to_dev_node(supplied, found));
506 * Opens the device node name passed and returns 1 (true) if the
507 * device is a CD.
510 static int
511 is_cd(char *node)
513 int fd;
514 struct dk_cinfo cinfo;
515 int ret = 1;
517 fd = open(node, O_RDONLY|O_NDELAY);
518 if (fd < 0) {
519 ret = 0;
520 } else if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
521 ret = 0;
522 } else if (cinfo.dki_ctype != DKC_CDROM) {
523 ret = 0;
526 if (fd >= 0) {
527 (void) close(fd);
529 return (ret);
532 static void
533 print_header(void)
535 /* l10n_NOTE : Column spacing should be kept same */
536 (void) printf(gettext(" Node Connected Device"));
537 /* l10n_NOTE : Column spacing should be kept same */
538 (void) printf(gettext(" Device type\n"));
539 (void) printf(
540 "----------------------+--------------------------------");
541 (void) printf("+-----------------\n");
545 * returns the number of writers or CD/DVD-roms found and the path of
546 * the first device found depending on the mode argument.
547 * possible mode values are:
548 * SCAN_ALL_CDS Scan all CD/DVD devices. Return first CD-RW found.
549 * SCAN_WRITERS Scan all CD-RW devices. Return first one found.
550 * SCAN_LISTDEVS List all devices found.
553 scan_for_cd_device(int mode, cd_device **found)
555 DIR *dir;
556 struct dirent *dirent;
557 char sdev[PATH_MAX], dev[PATH_MAX];
558 cd_device *t_dev;
559 int writers_found = 0;
560 int header_printed = 0;
561 int is_writer;
562 int total_devices_found;
564 TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode));
566 if (mode) {
567 (void) printf(gettext("Looking for CD devices...\n"));
570 dir = opendir("/dev/rdsk");
571 if (dir == NULL)
572 return (0);
574 writers_found = 0;
575 total_devices_found = 0;
576 while ((dirent = readdir(dir)) != NULL) {
577 if (dirent->d_name[0] == '.')
578 continue;
579 (void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
580 dirent->d_name);
581 if (strcmp("s2", (char *)strrchr(sdev, 's')) != 0)
582 continue;
583 if (!lookup_device(sdev, dev))
584 continue;
585 if (!is_cd(dev))
586 continue;
587 if ((t_dev = get_device(NULL, dev)) == NULL) {
588 continue;
590 total_devices_found++;
592 is_writer = !(check_device(t_dev, CHECK_DEVICE_NOT_WRITABLE));
594 if (is_writer) {
595 writers_found++;
597 if ((writers_found == 1) && (mode != SCAN_LISTDEVS)) {
598 *found = t_dev;
601 } else if ((mode == SCAN_ALL_CDS) && (writers_found == 0) &&
602 (total_devices_found == 1) && found) {
604 /* We found a CD-ROM or DVD-ROM */
605 *found = t_dev;
608 if (mode == SCAN_LISTDEVS) {
609 char *sn;
611 sn = hald_symname(sdev);
612 if (!header_printed) {
613 print_header();
614 header_printed = 1;
616 /* show vendor, model, firmware rev and device type */
617 (void) printf(" %-21.21s| %.8s %.16s %.4s | %s%s\n",
618 sn ? sn : sdev, &t_dev->d_inq[8],
619 &t_dev->d_inq[16], &t_dev->d_inq[32],
620 gettext("CD Reader"),
621 is_writer ? gettext("/Writer") : "");
622 free(sn);
624 if ((found != NULL) && ((*found) != t_dev))
625 fini_device(t_dev);
628 (void) closedir(dir);
630 if ((mode & SCAN_WRITERS) || writers_found)
631 return (writers_found);
632 else
633 return (total_devices_found);
637 * Check device for various conditions/capabilities
638 * If EXIT_IF_CHECK_FAILED set in cond then it will also exit after
639 * printing a message.
642 check_device(cd_device *dev, int cond)
644 uchar_t *disc_info, disc_status = 0, erasable = 0;
645 uchar_t page_code[4];
646 char *errmsg = NULL;
648 if ((errmsg == NULL) && (cond & CHECK_TYPE_NOT_CDROM) &&
649 ((dev->d_inq[0] & 0x1f) != 5)) {
650 errmsg =
651 gettext("Specified device does not appear to be a CDROM");
654 if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_READY) &&
655 !test_unit_ready(dev->d_fd)) {
656 errmsg = gettext("Device not ready");
659 /* Look at the capabilities page for this information */
660 if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_WRITABLE)) {
661 if (!get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) ||
662 ((page_code[3] & 1) == 0)) {
663 errmsg = gettext("Target device is not a CD writer");
667 if ((errmsg == NULL) && (cond & CHECK_NO_MEDIA)) {
668 if (!test_unit_ready(dev->d_fd) && (uscsi_status == 2) &&
669 ((RQBUFLEN - rqresid) >= 14) &&
670 ((SENSE_KEY(rqbuf) & 0x0f) == 2) && (ASC(rqbuf) == 0x3A) &&
671 ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
672 (ASCQ(rqbuf) == 2))) {
673 /* medium not present */
674 errmsg = gettext("No media in device");
680 /* Issue READ DISC INFORMATION mmc command */
681 if ((errmsg == NULL) && ((cond & CHECK_MEDIA_IS_NOT_BLANK) ||
682 (cond & CHECK_MEDIA_IS_NOT_WRITABLE) ||
683 (cond & CHECK_MEDIA_IS_NOT_ERASABLE))) {
685 disc_info = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
686 if (!read_disc_info(dev->d_fd, disc_info)) {
687 errmsg = gettext("Cannot obtain disc information");
688 } else {
689 disc_status = disc_info[2] & 0x03;
690 erasable = disc_info[2] & 0x10;
692 free(disc_info);
693 if (errmsg == NULL) {
694 if (!erasable && (cond & CHECK_MEDIA_IS_NOT_ERASABLE))
695 errmsg = gettext(
696 "Media in the device is not erasable");
697 else if ((disc_status != 0) &&
698 (cond & CHECK_MEDIA_IS_NOT_BLANK))
699 errmsg = gettext(
700 "Media in the device is not blank");
701 else if ((disc_status == 2) &&
702 (cond & CHECK_MEDIA_IS_NOT_WRITABLE) &&
703 ((device_type != DVD_PLUS_W) &&
704 (device_type != DVD_PLUS)))
705 errmsg = gettext(
706 "Media in the device is not writable");
710 if (errmsg) {
711 if (cond & EXIT_IF_CHECK_FAILED) {
712 err_msg("%s.\n", errmsg);
713 exit(1);
715 return (1);
717 return (0);
721 * Generic routine for writing whatever the next track is and taking
722 * care of the progress bar. Mode tells the track type (audio or data).
723 * Data from track is taken from the byte stream h
725 void
726 write_next_track(int mode, bstreamhandle h)
728 struct track_info *ti;
729 struct trackio_error *te;
730 off_t size;
732 ti = (struct track_info *)my_zalloc(sizeof (*ti));
733 if ((build_track_info(target, -1, ti) == 0) ||
734 ((ti->ti_flags & TI_NWA_VALID) == 0)) {
735 if ((device_type == DVD_PLUS) || (device_type ==
736 DVD_PLUS_W)) {
737 ti->ti_flags |= TI_NWA_VALID;
738 } else {
739 err_msg(gettext(
740 "Cannot get writable address for the media.\n"));
741 exit(1);
744 if (ti->ti_nwa != ti->ti_start_address) {
745 err_msg(gettext(
746 "Media state is not suitable for this write mode.\n"));
747 exit(1);
749 if (mode == TRACK_MODE_DATA) {
750 if (!(ti->ti_track_mode & 4)) {
751 /* Write track depends upon this bit */
752 ti->ti_track_mode |= TRACK_MODE_DATA;
755 size = 0;
756 h->bstr_size(h, &size);
757 h->bstr_rewind(h);
758 te = (struct trackio_error *)my_zalloc(sizeof (*te));
760 print_n_flush(gettext("Writing track %d..."), (int)ti->ti_track_no);
761 init_progress();
762 if (!write_track(target, ti, h, progress, size, te)) {
763 if (te->err_type == TRACKIO_ERR_USER_ABORT) {
764 (void) str_print(gettext("Aborted.\n"), progress_pos);
765 } else {
766 if (device_type != DVD_PLUS_W) {
767 /* l10n_NOTE : 'failed' as in Writing Track...failed */
768 (void) str_print(gettext("failed.\n"),
769 progress_pos);
773 /* l10n_NOTE : 'done' as in "Writing track 1...done" */
774 (void) str_print(gettext("done.\n"), progress_pos);
775 free(ti);
776 free(te);
779 void
780 list(void)
782 if (scan_for_cd_device(SCAN_LISTDEVS, NULL) == 0) {
783 if (vol_running) {
784 err_msg(gettext(
785 "No CD writers found, no media in the drive "
786 "or not on the console.\n"));
787 } else {
788 if (cur_uid != 0) {
789 err_msg(gettext(
790 "Volume manager is not running.\n"));
791 err_msg(gettext(
792 "Please start volume manager or run cdrw as root to access all devices.\n"));
793 } else {
794 err_msg(gettext("No CD writers found.\n"));
798 exit(0);
801 void
802 get_media_type(int fd)
804 uchar_t *cap = (uchar_t *)my_zalloc(MMC_FTR_HDR_LEN);
806 if (get_configuration(fd, MMC_FTR_PRFL_LIST,
807 MMC_FTR_HDR_LEN, cap)) {
808 if (debug)
809 (void) print_profile_list(fd);
810 switch (read_scsi16(&cap[6])) {
811 case 0x8: /* CD-ROM */
812 if (debug)
813 (void) printf("CD-ROM found\n");
815 * To avoid regression issues, treat as
816 * A cdrw, we will check the writable
817 * mode page to see if the media is
818 * actually writable.
820 device_type = CD_RW;
821 break;
823 case 0x9: /* CD-R */
824 if (debug)
825 (void) printf("CD-R found\n");
826 device_type = CD_RW;
827 break;
829 case 0x10: /* DVD-ROM */
831 * Have seen drives return DVD+RW media
832 * DVD-ROM, so try treating it as a DVD+RW
833 * profile. checking for writable media
834 * is done through mode page 5.
836 if (debug)
837 (void) printf("DVD-ROM found\n");
838 device_type = DVD_PLUS_W;
839 break;
841 case 0xA: /* CD-RW */
842 if (debug)
843 (void) printf("CD-RW found\n");
844 device_type = CD_RW;
845 break;
847 case 0x11: /* DVD-R */
848 if (debug)
849 (void) printf("DVD-R found\n");
850 device_type = DVD_MINUS;
851 break;
853 case 0x12: /* DVD-RAM */
854 if (debug)
855 (void) printf("DVD-RAM found\n");
856 /* treat as CD-RW, may be a legacy drive */
857 device_type = CD_RW;
858 break;
860 case 0x13: /* DVD-RW restricted overwrite */
861 case 0x14: /* DVD-RW sequential */
862 if (debug)
863 (void) printf("DVD-RW found\n");
864 device_type = DVD_MINUS;
865 break;
867 case 0x15: /* DVD-R Dual Layer Sequential Recording */
868 case 0x16: /* DVD-R Dual Layer Jump Recording */
869 if (debug)
870 (void) printf("DVD-R DL found\n");
871 device_type = DVD_MINUS;
872 break;
874 case 0x17: /* DVD-RW Dual Layer */
875 if (debug)
876 (void) printf("DVD-RW DL found\n");
877 device_type = DVD_MINUS;
878 break;
880 case 0x1A: /* DVD+RW */
881 if (debug)
882 (void) printf("DVD+RW found\n");
884 device_type = DVD_PLUS_W;
885 break;
887 case 0x1B: /* DVD+R */
888 if (debug)
889 (void) printf("DVD+R found\n");
890 device_type = DVD_PLUS;
891 break;
893 case 0x2A: /* DVD+RW Dual Layer */
894 if (debug)
895 (void) printf("DVD+RW DL found\n");
896 device_type = DVD_PLUS_W;
897 break;
899 case 0x2B: /* DVD+R Dual Layer */
900 if (debug)
901 (void) printf("DVD+R DL found\n");
902 device_type = DVD_PLUS;
903 break;
905 default:
906 if (debug)
907 (void) printf(
908 "unknown drive found\n type = 0x%x",
909 cap[7]);
911 * Treat as CD_RW to avoid regression, may
912 * be a legacy drive.
914 device_type = CD_RW;
917 free(cap);
920 /* Translate a transfer rate (eg, KB/s) into a Speed (eg, "2X") */
921 uint_t
922 cdrw_bandwidth_to_x(uint_t rate)
924 switch (device_type) {
925 case DVD_PLUS_W:
926 case DVD_MINUS:
927 case DVD_PLUS:
928 return (DVD_RATE_TO_X(rate));
930 default:
931 case CD_RW:
932 return (CD_RATE_TO_X(rate));
936 /* Translate a Speed (eg, "2X") into a transfer rate (eg, KB/s) */
937 uint_t
938 cdrw_x_to_bandwidth(uint_t x)
940 switch (device_type) {
941 case DVD_PLUS_W:
942 case DVD_MINUS:
943 case DVD_PLUS:
944 return (DVD_X_TO_RATE(x));
946 default:
947 case CD_RW:
948 return (CD_X_TO_RATE(x));