17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*5d465cc7Sminht * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <stdlib.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <stdio.h> 337c478bd9Sstevel@tonic-gate #include <sys/dkio.h> 347c478bd9Sstevel@tonic-gate #include <unistd.h> 357c478bd9Sstevel@tonic-gate #include <errno.h> 367c478bd9Sstevel@tonic-gate #include <libintl.h> 377c478bd9Sstevel@tonic-gate #include <sys/time.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #include "mmc.h" 407c478bd9Sstevel@tonic-gate #include "util.h" 417c478bd9Sstevel@tonic-gate #include "misc_scsi.h" 427c478bd9Sstevel@tonic-gate #include "transport.h" 437c478bd9Sstevel@tonic-gate #include "main.h" 447c478bd9Sstevel@tonic-gate #include "toshiba.h" 457c478bd9Sstevel@tonic-gate #include "msgs.h" 4698584592Sarutz #include "device.h" 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate uint32_t 497c478bd9Sstevel@tonic-gate read_scsi32(void *addr) 507c478bd9Sstevel@tonic-gate { 517c478bd9Sstevel@tonic-gate uchar_t *ad = (uchar_t *)addr; 527c478bd9Sstevel@tonic-gate uint32_t ret; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate ret = ((((uint32_t)ad[0]) << 24) | (((uint32_t)ad[1]) << 16) | 557c478bd9Sstevel@tonic-gate (((uint32_t)ad[2]) << 8) | ad[3]); 567c478bd9Sstevel@tonic-gate return (ret); 577c478bd9Sstevel@tonic-gate } 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate uint16_t 607c478bd9Sstevel@tonic-gate read_scsi16(void *addr) 617c478bd9Sstevel@tonic-gate { 627c478bd9Sstevel@tonic-gate uchar_t *ad = (uchar_t *)addr; 637c478bd9Sstevel@tonic-gate uint16_t ret; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate ret = ((((uint16_t)ad[0]) << 8) | ad[1]); 667c478bd9Sstevel@tonic-gate return (ret); 677c478bd9Sstevel@tonic-gate } 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate void 707c478bd9Sstevel@tonic-gate load_scsi32(void *addr, uint32_t v) 717c478bd9Sstevel@tonic-gate { 727c478bd9Sstevel@tonic-gate uchar_t *ad = (uchar_t *)addr; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate ad[0] = (uchar_t)(v >> 24); 757c478bd9Sstevel@tonic-gate ad[1] = (uchar_t)(v >> 16); 767c478bd9Sstevel@tonic-gate ad[2] = (uchar_t)(v >> 8); 777c478bd9Sstevel@tonic-gate ad[3] = (uchar_t)v; 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate void 817c478bd9Sstevel@tonic-gate load_scsi16(void *addr, uint16_t v) 827c478bd9Sstevel@tonic-gate { 837c478bd9Sstevel@tonic-gate uchar_t *ad = (uchar_t *)addr; 847c478bd9Sstevel@tonic-gate ad[0] = (uchar_t)(v >> 8); 857c478bd9Sstevel@tonic-gate ad[1] = (uchar_t)v; 867c478bd9Sstevel@tonic-gate } 877c478bd9Sstevel@tonic-gate /* 887c478bd9Sstevel@tonic-gate * will get the mode page only i.e. will strip off the header. 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate int 917c478bd9Sstevel@tonic-gate get_mode_page(int fd, int page_no, int pc, int buf_len, uchar_t *buffer) 927c478bd9Sstevel@tonic-gate { 937c478bd9Sstevel@tonic-gate int ret; 947c478bd9Sstevel@tonic-gate uchar_t byte2, *buf; 957c478bd9Sstevel@tonic-gate uint_t header_len, page_len, copy_cnt; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate byte2 = (uchar_t)(((pc << 6) & 0xC0) | (page_no & 0x3f)); 987c478bd9Sstevel@tonic-gate buf = (uchar_t *)my_zalloc(256); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* Ask 254 bytes only to make our IDE driver happy */ 1017c478bd9Sstevel@tonic-gate ret = mode_sense(fd, byte2, 1, 254, buf); 1027c478bd9Sstevel@tonic-gate if (ret == 0) { 1037c478bd9Sstevel@tonic-gate free(buf); 1047c478bd9Sstevel@tonic-gate return (0); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate header_len = 8 + read_scsi16(&buf[6]); 1087c478bd9Sstevel@tonic-gate page_len = buf[header_len + 1] + 2; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate copy_cnt = (page_len > buf_len) ? buf_len : page_len; 1117c478bd9Sstevel@tonic-gate (void) memcpy(buffer, &buf[header_len], copy_cnt); 1127c478bd9Sstevel@tonic-gate free(buf); 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate return (1); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * will take care of adding mode header and any extra bytes at the end. 1197c478bd9Sstevel@tonic-gate */ 1207c478bd9Sstevel@tonic-gate int 1217c478bd9Sstevel@tonic-gate set_mode_page(int fd, uchar_t *buffer) 1227c478bd9Sstevel@tonic-gate { 1237c478bd9Sstevel@tonic-gate int ret; 1247c478bd9Sstevel@tonic-gate uchar_t *buf; 1257c478bd9Sstevel@tonic-gate uint_t total, p_len; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate p_len = buffer[1] + 2; 1287c478bd9Sstevel@tonic-gate total = p_len + 8; 1297c478bd9Sstevel@tonic-gate buf = (uchar_t *)my_zalloc(total); 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate (void) memcpy(&buf[8], buffer, p_len); 1327c478bd9Sstevel@tonic-gate if (debug) { 1337c478bd9Sstevel@tonic-gate int i; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate (void) printf("MODE: ["); 1367c478bd9Sstevel@tonic-gate for (i = 0; i < p_len; i++) { 1377c478bd9Sstevel@tonic-gate (void) printf("0x%02x ", (uchar_t)buffer[i]); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate (void) printf("]\n"); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate ret = mode_select(fd, total, buf); 1437c478bd9Sstevel@tonic-gate free(buf); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate return (ret); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* 1497c478bd9Sstevel@tonic-gate * Builds track information database for track trackno. If trackno is 1507c478bd9Sstevel@tonic-gate * -1, builds the database for next blank track. 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate int 1537c478bd9Sstevel@tonic-gate build_track_info(cd_device *dev, int trackno, struct track_info *t_info) 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate uchar_t *ti; 1567c478bd9Sstevel@tonic-gate uchar_t toc[20]; /* 2 entries + 4 byte header */ 1577c478bd9Sstevel@tonic-gate int ret; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate (void) memset(t_info, 0, sizeof (*t_info)); 1607c478bd9Sstevel@tonic-gate /* 1st try READ TRACK INFORMATION */ 1617c478bd9Sstevel@tonic-gate ti = (uchar_t *)my_zalloc(TRACK_INFO_SIZE); 1627c478bd9Sstevel@tonic-gate t_info->ti_track_no = trackno; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* Gererate faked information for writing to DVD */ 1657c478bd9Sstevel@tonic-gate if (device_type != CD_RW) { 1667c478bd9Sstevel@tonic-gate uint_t bsize; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate t_info->ti_flags = 0x3000; 1697c478bd9Sstevel@tonic-gate t_info->ti_track_no = 1; 1707c478bd9Sstevel@tonic-gate t_info->ti_session_no = 1; 1717c478bd9Sstevel@tonic-gate t_info->ti_track_mode = 0x4; 1727c478bd9Sstevel@tonic-gate t_info->ti_data_mode = 1; 1737c478bd9Sstevel@tonic-gate t_info->ti_start_address = 0; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* only 1 track on DVD make it max size */ 1767c478bd9Sstevel@tonic-gate t_info->ti_track_size = read_format_capacity(target->d_fd, 1777c478bd9Sstevel@tonic-gate &bsize); 1787c478bd9Sstevel@tonic-gate if (t_info->ti_track_size < MAX_CD_BLKS) { 1797c478bd9Sstevel@tonic-gate t_info->ti_track_size = MAX_DVD_BLKS; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate t_info->ti_nwa = 0; 1837c478bd9Sstevel@tonic-gate t_info->ti_lra = 0; 1847c478bd9Sstevel@tonic-gate t_info->ti_packet_size = 0x10; 1857c478bd9Sstevel@tonic-gate t_info->ti_free_blocks = 0; 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate if (read_track_info(dev->d_fd, trackno, ti)) { 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate if (debug) 1917c478bd9Sstevel@tonic-gate (void) printf("using read_track_info for TOC \n"); 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate t_info->ti_track_no = ti[2]; 1947c478bd9Sstevel@tonic-gate t_info->ti_session_no = ti[3]; 1957c478bd9Sstevel@tonic-gate t_info->ti_flags = (ti[6] >> 4) & 0xf; 1967c478bd9Sstevel@tonic-gate t_info->ti_flags |= (uint32_t)(ti[5] & 0xf0); 1977c478bd9Sstevel@tonic-gate t_info->ti_flags |= (uint32_t)(ti[7]) << 8; 1987c478bd9Sstevel@tonic-gate t_info->ti_flags |= TI_SESSION_NO_VALID | TI_FREE_BLOCKS_VALID; 1997c478bd9Sstevel@tonic-gate t_info->ti_track_mode = ti[5] & 0xf; 2007c478bd9Sstevel@tonic-gate if ((ti[6] & 0xf) == 0xf) 2017c478bd9Sstevel@tonic-gate t_info->ti_data_mode = 0xff; 2027c478bd9Sstevel@tonic-gate else 2037c478bd9Sstevel@tonic-gate t_info->ti_data_mode = ti[6] & 0xf; 2047c478bd9Sstevel@tonic-gate t_info->ti_start_address = read_scsi32(&ti[8]); 2057c478bd9Sstevel@tonic-gate t_info->ti_nwa = read_scsi32(&ti[12]); 2067c478bd9Sstevel@tonic-gate t_info->ti_free_blocks = read_scsi32(&ti[16]); 2077c478bd9Sstevel@tonic-gate t_info->ti_packet_size = read_scsi32(&ti[20]); 2087c478bd9Sstevel@tonic-gate t_info->ti_track_size = read_scsi32(&ti[24]); 2097c478bd9Sstevel@tonic-gate t_info->ti_lra = read_scsi32(&ti[28]); 2107c478bd9Sstevel@tonic-gate free(ti); 2117c478bd9Sstevel@tonic-gate return (1); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate /* READ TRACK INFORMATION not supported, try other options */ 2147c478bd9Sstevel@tonic-gate free(ti); 2157c478bd9Sstevel@tonic-gate /* 2167c478bd9Sstevel@tonic-gate * We can get info for next blank track if READ TRACK INFO is not 2177c478bd9Sstevel@tonic-gate * supported. 2187c478bd9Sstevel@tonic-gate */ 2197c478bd9Sstevel@tonic-gate if (trackno == -1) 2207c478bd9Sstevel@tonic-gate return (0); 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate if (debug) 2237c478bd9Sstevel@tonic-gate (void) printf("using READ_TOC for TOC\n"); 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /* Try Read TOC */ 2267c478bd9Sstevel@tonic-gate if (!read_toc(dev->d_fd, 0, trackno, 20, toc)) { 2277c478bd9Sstevel@tonic-gate return (0); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate t_info->ti_start_address = read_scsi32(&toc[8]); 2307c478bd9Sstevel@tonic-gate t_info->ti_track_mode = toc[5] & 0xf; 2317c478bd9Sstevel@tonic-gate t_info->ti_track_size = read_scsi32(&toc[16]) - read_scsi32(&toc[8]); 2327c478bd9Sstevel@tonic-gate t_info->ti_data_mode = get_data_mode(dev->d_fd, read_scsi32(&toc[8])); 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* Numbers for audio tracks are always in 2K chunks */ 2357c478bd9Sstevel@tonic-gate if ((dev->d_blksize == 512) && ((t_info->ti_track_mode & 4) == 0)) { 2367c478bd9Sstevel@tonic-gate t_info->ti_start_address /= 4; 2377c478bd9Sstevel@tonic-gate t_info->ti_track_size /= 4; 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate /* Now find out the session thing */ 2417c478bd9Sstevel@tonic-gate ret = read_toc(dev->d_fd, 1, trackno, 12, toc); 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate /* 2447c478bd9Sstevel@tonic-gate * Make sure that the call succeeds and returns the requested 2457c478bd9Sstevel@tonic-gate * TOC size correctly. 2467c478bd9Sstevel@tonic-gate */ 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate if ((ret == 0) || (toc[1] != 0x0a)) { 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate /* For ATAPI drives or old Toshiba drives */ 2517c478bd9Sstevel@tonic-gate ret = read_toc_as_per_8020(dev->d_fd, 1, trackno, 12, toc); 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate /* If this goes through well TOC length will always be 0x0a */ 2547c478bd9Sstevel@tonic-gate if (ret && (toc[1] == 0x0a)) { 2557c478bd9Sstevel@tonic-gate if (trackno >= toc[6]) { 2567c478bd9Sstevel@tonic-gate t_info->ti_session_no = toc[3]; 2577c478bd9Sstevel@tonic-gate t_info->ti_flags |= TI_SESSION_NO_VALID; 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate /* 2607c478bd9Sstevel@tonic-gate * This might be the last track of this session. If so, 2617c478bd9Sstevel@tonic-gate * exclude the leadout and next lead in. 2627c478bd9Sstevel@tonic-gate */ 2637c478bd9Sstevel@tonic-gate if (trackno == (toc[6] - 1)) { 2647c478bd9Sstevel@tonic-gate /* 2657c478bd9Sstevel@tonic-gate * 1.5 Min leadout + 1 min. leadin + 2 sec. pre-gap. 2667c478bd9Sstevel@tonic-gate * For 2nd+ leadout it will be 0.5 min. But currently 2677c478bd9Sstevel@tonic-gate * there is no direct way. And it will not happen 2687c478bd9Sstevel@tonic-gate * for any normal case. 2697c478bd9Sstevel@tonic-gate * 2707c478bd9Sstevel@tonic-gate * 75 frames/sec, 60 sec/min, so leadin gap is 2717c478bd9Sstevel@tonic-gate * ((1.5 +1)*60 + 2)*75 = 11400 frames (blocks) 2727c478bd9Sstevel@tonic-gate */ 2737c478bd9Sstevel@tonic-gate t_info->ti_track_size -= 11400; 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate return (1); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate uchar_t 2807c478bd9Sstevel@tonic-gate get_data_mode(int fd, uint32_t lba) 2817c478bd9Sstevel@tonic-gate { 2827c478bd9Sstevel@tonic-gate int ret; 2837c478bd9Sstevel@tonic-gate uchar_t *buf; 2847c478bd9Sstevel@tonic-gate uchar_t mode; 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate buf = (uchar_t *)my_zalloc(8); 2877c478bd9Sstevel@tonic-gate ret = read_header(fd, lba, buf); 2887c478bd9Sstevel@tonic-gate if (ret == 0) 2897c478bd9Sstevel@tonic-gate mode = 0xff; 2907c478bd9Sstevel@tonic-gate else 2917c478bd9Sstevel@tonic-gate mode = buf[0]; 2927c478bd9Sstevel@tonic-gate free(buf); 2937c478bd9Sstevel@tonic-gate return (mode); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate /* 2977c478bd9Sstevel@tonic-gate * Set page code 5 for TAO mode. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate int 3007c478bd9Sstevel@tonic-gate prepare_for_write(cd_device *dev, int track_mode, int test_write, 3017c478bd9Sstevel@tonic-gate int keep_disc_open) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate uchar_t *buf; 3047c478bd9Sstevel@tonic-gate int no_err; 3057c478bd9Sstevel@tonic-gate int reset_device; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate if ((write_mode == DAO_MODE) && keep_disc_open) { 3087c478bd9Sstevel@tonic-gate (void) printf(gettext( 3097c478bd9Sstevel@tonic-gate "Multi-session is not supported on DVD media\n")); 3107c478bd9Sstevel@tonic-gate exit(1); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate if ((write_mode == DAO_MODE) && debug) { 3147c478bd9Sstevel@tonic-gate (void) printf("Preparing to write in DAO\n"); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate (void) start_stop(dev->d_fd, 1); 3187c478bd9Sstevel@tonic-gate /* Some drives do not support this command but still do it */ 3197c478bd9Sstevel@tonic-gate (void) rezero_unit(dev->d_fd); 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate buf = (uchar_t *)my_zalloc(64); 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate no_err = get_mode_page(dev->d_fd, 5, 0, 64, buf); 3247c478bd9Sstevel@tonic-gate if (no_err) 3257c478bd9Sstevel@tonic-gate no_err = ((buf[1] + 2) > 64) ? 0 : 1; 3267c478bd9Sstevel@tonic-gate /* 3277c478bd9Sstevel@tonic-gate * If the device is already in simulation mode and again a 3287c478bd9Sstevel@tonic-gate * simulation is requested, then set the device in non-simulation 3297c478bd9Sstevel@tonic-gate * 1st and then take it to simulation mode. This will flush any 3307c478bd9Sstevel@tonic-gate * previous fake state in the drive. 3317c478bd9Sstevel@tonic-gate */ 3327c478bd9Sstevel@tonic-gate if (no_err && test_write && (buf[2] & 0x10)) { 3337c478bd9Sstevel@tonic-gate reset_device = 1; 3347c478bd9Sstevel@tonic-gate } else { 3357c478bd9Sstevel@tonic-gate reset_device = 0; 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate if (no_err != 0) { 3387c478bd9Sstevel@tonic-gate buf[0] &= 0x3f; 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /* set TAO or DAO writing mode */ 3417c478bd9Sstevel@tonic-gate buf[2] = (write_mode == TAO_MODE)?1:2; 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate /* set simulation flag */ 3447c478bd9Sstevel@tonic-gate if (test_write && (!reset_device)) { 3457c478bd9Sstevel@tonic-gate buf[2] |= 0x10; 3467c478bd9Sstevel@tonic-gate } else { 3477c478bd9Sstevel@tonic-gate buf[2] &= ~0x10; 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* Turn on HW buffer underrun protection (BUFE) */ 3517c478bd9Sstevel@tonic-gate if (!test_write) { 3527c478bd9Sstevel@tonic-gate buf[2] |= 0x40; 3537c478bd9Sstevel@tonic-gate } 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate /* set track mode type */ 3567c478bd9Sstevel@tonic-gate if (device_type == CD_RW) { 3577c478bd9Sstevel@tonic-gate buf[3] = track_mode & 0x0f; /* ctrl nibble */ 3587c478bd9Sstevel@tonic-gate } else { 3597c478bd9Sstevel@tonic-gate buf[3] = 5; /* always 5 for DVD */ 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate if (keep_disc_open) { 3637c478bd9Sstevel@tonic-gate buf[3] |= 0xc0; /* Allow more sessions */ 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* Select track type (audio or data) */ 3677c478bd9Sstevel@tonic-gate if (track_mode == TRACK_MODE_DATA) { 3687c478bd9Sstevel@tonic-gate buf[4] = 8; /* 2048 byte sector */ 3697c478bd9Sstevel@tonic-gate } else { 3707c478bd9Sstevel@tonic-gate buf[4] = 0; /* 2352 byte sector */ 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate buf[7] = buf[8] = 0; 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate /* Need to clear these fields for setting into DAO */ 3757c478bd9Sstevel@tonic-gate if (write_mode == DAO_MODE) 3767c478bd9Sstevel@tonic-gate buf[5] = buf[15] = 0; 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate /* print out mode for detailed log */ 3797c478bd9Sstevel@tonic-gate if (debug && verbose) { 3807c478bd9Sstevel@tonic-gate int i; 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate (void) printf("setting = [ "); 3837c478bd9Sstevel@tonic-gate for (i = 0; i < 15; i++) 3847c478bd9Sstevel@tonic-gate (void) printf("0x%x ", buf[i]); 3857c478bd9Sstevel@tonic-gate (void) printf("]\n"); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate no_err = set_mode_page(dev->d_fd, buf); 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate if (no_err && reset_device) { 3917c478bd9Sstevel@tonic-gate /* Turn the test write bit back on */ 3927c478bd9Sstevel@tonic-gate buf[2] |= 0x10; 3937c478bd9Sstevel@tonic-gate no_err = set_mode_page(dev->d_fd, buf); 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate /* 3977c478bd9Sstevel@tonic-gate * Since BUFE is the only optional flag we are 3987c478bd9Sstevel@tonic-gate * setting we will try to turn it off if the command 3997c478bd9Sstevel@tonic-gate * fails. 4007c478bd9Sstevel@tonic-gate */ 4017c478bd9Sstevel@tonic-gate if (!no_err) { 4027c478bd9Sstevel@tonic-gate /* 4037c478bd9Sstevel@tonic-gate * Some old drives may not support HW 4047c478bd9Sstevel@tonic-gate * buffer underrun protection, try again 4057c478bd9Sstevel@tonic-gate * after turning it off. 4067c478bd9Sstevel@tonic-gate */ 4077c478bd9Sstevel@tonic-gate if (debug) 4087c478bd9Sstevel@tonic-gate (void) printf("Turning off BUFE\n"); 4097c478bd9Sstevel@tonic-gate buf[2] &= ~0x40; 4107c478bd9Sstevel@tonic-gate no_err = set_mode_page(dev->d_fd, buf); 4117c478bd9Sstevel@tonic-gate } 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate free(buf); 4157c478bd9Sstevel@tonic-gate return (no_err); 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate /* 4197c478bd9Sstevel@tonic-gate * Close session. This will write TOC. 4207c478bd9Sstevel@tonic-gate */ 4217c478bd9Sstevel@tonic-gate int 4227c478bd9Sstevel@tonic-gate finalize(cd_device *dev) 4237c478bd9Sstevel@tonic-gate { 4247c478bd9Sstevel@tonic-gate uchar_t *di; 4257c478bd9Sstevel@tonic-gate int count, ret, err; 4267c478bd9Sstevel@tonic-gate int immediate; 4277c478bd9Sstevel@tonic-gate int finalize_max; 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate /* 4307c478bd9Sstevel@tonic-gate * For ATAPI devices we will use the immediate mode and will 4317c478bd9Sstevel@tonic-gate * poll the command for completion so that this command may 4327c478bd9Sstevel@tonic-gate * not hog the channel. But for SCSI, we will use the treditional 4337c478bd9Sstevel@tonic-gate * way of issuing the command with a large enough timeout. This 4347c478bd9Sstevel@tonic-gate * is done because immediate mode was designed for ATAPI and some 4357c478bd9Sstevel@tonic-gate * SCSI RW drives might not be even tested with it. 4367c478bd9Sstevel@tonic-gate */ 4377c478bd9Sstevel@tonic-gate if ((dev->d_inq[2] & 7) != 0) { 4387c478bd9Sstevel@tonic-gate /* SCSI device */ 4397c478bd9Sstevel@tonic-gate immediate = 0; 4407c478bd9Sstevel@tonic-gate } else { 4417c478bd9Sstevel@tonic-gate /* non-SCSI (e.g ATAPI) device */ 4427c478bd9Sstevel@tonic-gate immediate = 1; 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate /* We need to close track before close session */ 4467c478bd9Sstevel@tonic-gate if (device_type == DVD_PLUS) { 4477c478bd9Sstevel@tonic-gate if (!close_track(dev->d_fd, 0, 0, immediate)) 4487c478bd9Sstevel@tonic-gate return (0); 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate if (!close_track(dev->d_fd, 0, 1, immediate)) { 4527c478bd9Sstevel@tonic-gate /* 453*5d465cc7Sminht * For DAO mode which we use for DVD-RW, the latest MMC 454*5d465cc7Sminht * specification does not mention close_track. Some 455*5d465cc7Sminht * newer drives will return an ILLEGAL INSTRUCTION 456*5d465cc7Sminht * which we will ignore. We have also found a Panasonic 457*5d465cc7Sminht * drive which will return a MEDIA ERROR. It is safe 458*5d465cc7Sminht * to ignore both errors as this is not needed for 459*5d465cc7Sminht * these drives. 460*5d465cc7Sminht * This is kept for older drives which had needed 461*5d465cc7Sminht * us to issue close_track to flush the cache fully. 462*5d465cc7Sminht * once we are certain these drives have cleared the 463*5d465cc7Sminht * market, this can be removed. 4647c478bd9Sstevel@tonic-gate */ 4657c478bd9Sstevel@tonic-gate if (device_type == DVD_MINUS) { 4667c478bd9Sstevel@tonic-gate return (0); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate } else { 4697c478bd9Sstevel@tonic-gate if (!immediate) 4707c478bd9Sstevel@tonic-gate return (1); 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate if (immediate) { 4737c478bd9Sstevel@tonic-gate (void) sleep(10); 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE); 4767c478bd9Sstevel@tonic-gate err = 0; 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate if (device_type == CD_RW) { 4797c478bd9Sstevel@tonic-gate /* Finalization should not take more than 6 minutes */ 4807c478bd9Sstevel@tonic-gate finalize_max = FINALIZE_TIMEOUT; 4817c478bd9Sstevel@tonic-gate } else { 4827c478bd9Sstevel@tonic-gate /* some DVD-RW drives take longer than 6 minutes */ 4837c478bd9Sstevel@tonic-gate finalize_max = FINALIZE_TIMEOUT*2; 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate for (count = 0; count < finalize_max; count++) { 4877c478bd9Sstevel@tonic-gate ret = read_disc_info(dev->d_fd, di); 4887c478bd9Sstevel@tonic-gate if (ret != 0) 4897c478bd9Sstevel@tonic-gate break; 4907c478bd9Sstevel@tonic-gate if (uscsi_status != 2) 4917c478bd9Sstevel@tonic-gate err = 1; 4927c478bd9Sstevel@tonic-gate if (SENSE_KEY(rqbuf) == 2) { 4937c478bd9Sstevel@tonic-gate /* not ready but not becoming ready */ 4947c478bd9Sstevel@tonic-gate if (ASC(rqbuf) != 4) 4957c478bd9Sstevel@tonic-gate err = 1; 4967c478bd9Sstevel@tonic-gate } else if (SENSE_KEY(rqbuf) == 5) { 4977c478bd9Sstevel@tonic-gate /* illegal mode for this track */ 4987c478bd9Sstevel@tonic-gate if (ASC(rqbuf) != 0x64) 4997c478bd9Sstevel@tonic-gate err = 1; 5007c478bd9Sstevel@tonic-gate } else { 5017c478bd9Sstevel@tonic-gate err = 1; 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate if (err == 1) { 5047c478bd9Sstevel@tonic-gate if (debug) { 5057c478bd9Sstevel@tonic-gate (void) printf("Finalization failed\n"); 5067c478bd9Sstevel@tonic-gate (void) printf("%x %x %x %x\n", 5077c478bd9Sstevel@tonic-gate uscsi_status, SENSE_KEY(rqbuf), 5087c478bd9Sstevel@tonic-gate ASC(rqbuf), ASCQ(rqbuf)); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate free(di); 5117c478bd9Sstevel@tonic-gate return (0); 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate if (uscsi_status == 2) { 5147c478bd9Sstevel@tonic-gate int i; 5157c478bd9Sstevel@tonic-gate /* illegal field in command packet */ 5167c478bd9Sstevel@tonic-gate if (ASC(rqbuf) == 0x24) { 5177c478bd9Sstevel@tonic-gate /* print it out! */ 5187c478bd9Sstevel@tonic-gate (void) printf("\n"); 5197c478bd9Sstevel@tonic-gate for (i = 0; i < 18; i++) 5207c478bd9Sstevel@tonic-gate (void) printf("%x ", 5217c478bd9Sstevel@tonic-gate (unsigned)(rqbuf[i])); 5227c478bd9Sstevel@tonic-gate (void) printf("\n"); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate (void) sleep(5); 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate free(di); 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate return (ret); 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate /* 5337c478bd9Sstevel@tonic-gate * Find out media capacity. 5347c478bd9Sstevel@tonic-gate */ 5357c478bd9Sstevel@tonic-gate int 5367c478bd9Sstevel@tonic-gate get_last_possible_lba(cd_device *dev) 5377c478bd9Sstevel@tonic-gate { 5387c478bd9Sstevel@tonic-gate uchar_t *di; 5397c478bd9Sstevel@tonic-gate int cap; 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE); 5427c478bd9Sstevel@tonic-gate if (!read_disc_info(dev->d_fd, di)) { 5437c478bd9Sstevel@tonic-gate free(di); 5447c478bd9Sstevel@tonic-gate return (0); 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate if ((di[21] != 0) && (di[21] != 0xff)) { 5477c478bd9Sstevel@tonic-gate cap = ((di[21] * 60) + di[22]) * 75; 5487c478bd9Sstevel@tonic-gate } else { 5497c478bd9Sstevel@tonic-gate cap = 0; 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate free(di); 5537c478bd9Sstevel@tonic-gate return (cap); 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate int 5577c478bd9Sstevel@tonic-gate read_audio_through_read_cd(cd_device *dev, uint_t start_lba, uint_t nblks, 5587c478bd9Sstevel@tonic-gate uchar_t *buf) 5597c478bd9Sstevel@tonic-gate { 5607c478bd9Sstevel@tonic-gate int retry; 5617c478bd9Sstevel@tonic-gate int ret; 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate for (retry = 0; retry < 3; retry++) { 5647c478bd9Sstevel@tonic-gate ret = read_cd(dev->d_fd, (uint32_t)start_lba, (uint16_t)nblks, 5657c478bd9Sstevel@tonic-gate 1, buf, (uint32_t)(nblks * 2352)); 5667c478bd9Sstevel@tonic-gate if (ret) 5677c478bd9Sstevel@tonic-gate break; 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate return (ret); 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate int 5737c478bd9Sstevel@tonic-gate eject_media(cd_device *dev) 5747c478bd9Sstevel@tonic-gate { 5757c478bd9Sstevel@tonic-gate if (vol_running) { 5767c478bd9Sstevel@tonic-gate /* If there is a media, try using DKIOCEJECT 1st */ 5777c478bd9Sstevel@tonic-gate if (check_device(dev, CHECK_NO_MEDIA) == 0) { 5787c478bd9Sstevel@tonic-gate if (ioctl(dev->d_fd, DKIOCEJECT, 0) == 0) { 5797c478bd9Sstevel@tonic-gate return (1); 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate } 5837c478bd9Sstevel@tonic-gate if (load_unload(dev->d_fd, 0) == 0) { 5847c478bd9Sstevel@tonic-gate /* if eject fails */ 5857c478bd9Sstevel@tonic-gate if ((uscsi_status == 2) && (ASC(rqbuf) == 0x53)) { 5867c478bd9Sstevel@tonic-gate /* 5877c478bd9Sstevel@tonic-gate * check that eject is not blocked on the device 5887c478bd9Sstevel@tonic-gate */ 5897c478bd9Sstevel@tonic-gate if (!prevent_allow_mr(dev->d_fd, 1)) 5907c478bd9Sstevel@tonic-gate return (0); 5917c478bd9Sstevel@tonic-gate return (load_unload(dev->d_fd, 0)); 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate return (0); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate return (1); 5967c478bd9Sstevel@tonic-gate } 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate /* 59998584592Sarutz * Get current Read or Write Speed from Mode Page 0x2a. 60098584592Sarutz * 60198584592Sarutz * Use the size of the Page to determine which Multimedia Command 60298584592Sarutz * set (MMC) is present. Based on the MMC version, get the 60398584592Sarutz * specified Read/Write Speed. 60498584592Sarutz * 60598584592Sarutz * Note that some MMC versions do not necessarily support a 60698584592Sarutz * (current) Read or Write Speed. As a result, this function 60798584592Sarutz * _can_ return a value of zero. 60898584592Sarutz * 60998584592Sarutz * The newer standards (reserve and) mark the field(s) as Obsolete, 61098584592Sarutz * yet many vendors populate the Obsolete fields with valid values 61198584592Sarutz * (assumedly for backward compatibility). This is important, as 61298584592Sarutz * a command like GET PERFORMANCE cannot return _the_ speed; it can 61398584592Sarutz * only return a Logical-Block-Address-dependent (LBA) speed. Such 61498584592Sarutz * values can vary widely between the innermost and outermost Track. 61598584592Sarutz * Mode Page 0x2a is the best solution identifying "the current 61698584592Sarutz * (nominal) speed". 6177c478bd9Sstevel@tonic-gate */ 6187c478bd9Sstevel@tonic-gate static uint16_t 61998584592Sarutz cd_speed_get(cd_device *dev, int cmd) 6207c478bd9Sstevel@tonic-gate { 6217c478bd9Sstevel@tonic-gate uchar_t *mp2a; 62298584592Sarutz uint16_t rate = 0; 62398584592Sarutz int offset; 62498584592Sarutz uint_t buflen = 254; 6257c478bd9Sstevel@tonic-gate 62698584592Sarutz /* 62798584592Sarutz * Allocate a buffer acceptably larger than any nominal 62898584592Sarutz * Page for Page Code 0x2A. 62998584592Sarutz */ 63098584592Sarutz mp2a = (uchar_t *)my_zalloc(buflen); 63198584592Sarutz if (get_mode_page(dev->d_fd, 0x2A, 0, buflen, mp2a) == 0) 63298584592Sarutz goto end; 63398584592Sarutz 63498584592Sarutz /* Determine MMC version based on 'Page Length' field */ 63598584592Sarutz switch (mp2a[1]) { 63698584592Sarutz case 0x14: /* MMC-1 */ 63798584592Sarutz if (debug) 63898584592Sarutz (void) printf("Mode Page 2A: MMC-1\n"); 63998584592Sarutz 64098584592Sarutz offset = (cmd == GET_READ_SPEED) ? 14 : 20; 64198584592Sarutz rate = read_scsi16(&mp2a[offset]); 64298584592Sarutz break; 64398584592Sarutz 64498584592Sarutz 64598584592Sarutz case 0x18: /* MMC-2 */ 64698584592Sarutz if (debug) 64798584592Sarutz (void) printf("Mode Page 2A: MMC-2;" 64898584592Sarutz " Read and Write Speeds are " 64998584592Sarutz "obsolete\n"); 65098584592Sarutz 65198584592Sarutz /* see if "Obsolete" values are valid: */ 65298584592Sarutz offset = (cmd == GET_READ_SPEED) ? 14 : 20; 65398584592Sarutz rate = read_scsi16(&mp2a[offset]); 65498584592Sarutz break; 65598584592Sarutz 65698584592Sarutz default: /* MMC-3 or newer */ 65798584592Sarutz if (debug) 65898584592Sarutz (void) printf("Mode Page 2A: MMC-3 or" 65998584592Sarutz " newer; Read Speed is obsolete.\n"); 66098584592Sarutz 6617c478bd9Sstevel@tonic-gate if (cmd == GET_READ_SPEED) { 66298584592Sarutz /* this is Obsolete, but try it */ 66398584592Sarutz offset = 14; 66498584592Sarutz rate = read_scsi16(&mp2a[offset]); 6657c478bd9Sstevel@tonic-gate } else { 66698584592Sarutz /* Write Speed is not obsolete */ 66798584592Sarutz offset = 28; 66898584592Sarutz rate = read_scsi16(&mp2a[offset]); 66998584592Sarutz 67098584592Sarutz if (rate == 0) { 67198584592Sarutz /* 67298584592Sarutz * then try an Obsolete field 67398584592Sarutz * (but this shouldn't happen!) 67498584592Sarutz */ 67598584592Sarutz offset = 20; 67698584592Sarutz rate = read_scsi16(&mp2a[offset]); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate } 67998584592Sarutz break; 68098584592Sarutz } 68198584592Sarutz end: 6827c478bd9Sstevel@tonic-gate free(mp2a); 68398584592Sarutz 68498584592Sarutz if (debug) 68598584592Sarutz (void) printf("cd_speed_get: %s Speed is " 68698584592Sarutz "%uX\n", (cmd == GET_READ_SPEED) ? 68798584592Sarutz "Read" : "Write", cdrw_bandwidth_to_x(rate)); 6887c478bd9Sstevel@tonic-gate return (rate); 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate /* 6927c478bd9Sstevel@tonic-gate * CD speed related functions (ioctl style) for drives which do not support 6937c478bd9Sstevel@tonic-gate * real time streaming. 69498584592Sarutz * 69598584592Sarutz * For the SET operations, the SET CD SPEED command needs 69698584592Sarutz * both the Read Speed and the Write Speed. Eg, if 69798584592Sarutz * we're trying to set the Write Speed (SET_WRITE_SPEED), 69898584592Sarutz * then we first need to obtain the current Read Speed. 69998584592Sarutz * That speed is specified along with the chosen_speed (the 70098584592Sarutz * Write Speed in this case) in the SET CD SPEED command. 7017c478bd9Sstevel@tonic-gate */ 7027c478bd9Sstevel@tonic-gate int 7037c478bd9Sstevel@tonic-gate cd_speed_ctrl(cd_device *dev, int cmd, int speed) 7047c478bd9Sstevel@tonic-gate { 7057c478bd9Sstevel@tonic-gate uint16_t rate; 7067c478bd9Sstevel@tonic-gate 70798584592Sarutz switch (cmd) { 70898584592Sarutz case GET_READ_SPEED: 70998584592Sarutz rate = cd_speed_get(dev, GET_READ_SPEED); 71098584592Sarutz return (cdrw_bandwidth_to_x(rate)); 71198584592Sarutz 71298584592Sarutz case GET_WRITE_SPEED: 71398584592Sarutz rate = cd_speed_get(dev, GET_WRITE_SPEED); 71498584592Sarutz return (cdrw_bandwidth_to_x(rate)); 71598584592Sarutz 71698584592Sarutz case SET_READ_SPEED: 71798584592Sarutz rate = cd_speed_get(dev, GET_WRITE_SPEED); 71898584592Sarutz return (set_cd_speed(dev->d_fd, 71998584592Sarutz cdrw_x_to_bandwidth(speed), rate)); 72098584592Sarutz break; 72198584592Sarutz 72298584592Sarutz case SET_WRITE_SPEED: 72398584592Sarutz rate = cd_speed_get(dev, GET_READ_SPEED); 7247c478bd9Sstevel@tonic-gate return (set_cd_speed(dev->d_fd, rate, 72598584592Sarutz cdrw_x_to_bandwidth(speed))); 72698584592Sarutz break; 72798584592Sarutz 72898584592Sarutz default: 7297c478bd9Sstevel@tonic-gate return (0); 7307c478bd9Sstevel@tonic-gate } 73198584592Sarutz } 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate /* 73498584592Sarutz * Manage sending of SET STREAMING command using the specified 73598584592Sarutz * read_speed and write_speed. 73698584592Sarutz * 73798584592Sarutz * This function allocates and initializes a Performance 73898584592Sarutz * Descriptor, which is sent as part of the SET STREAMING 73998584592Sarutz * command. The descriptor is deallocated before function 74098584592Sarutz * exit. 74198584592Sarutz */ 74298584592Sarutz static int 74398584592Sarutz do_set_streaming(cd_device *dev, uint_t read_speed, 74498584592Sarutz uint_t write_speed) 74598584592Sarutz { 74698584592Sarutz int ret; 74798584592Sarutz uchar_t *str; 74898584592Sarutz 74998584592Sarutz /* Allocate and initialize the Performance Descriptor */ 75098584592Sarutz str = (uchar_t *)my_zalloc(SET_STREAM_DATA_LEN); 75198584592Sarutz 75298584592Sarutz /* Read Time (in milliseconds) */ 75398584592Sarutz load_scsi32(&str[16], 1000); 75498584592Sarutz /* Write Time (in milliseconds) */ 75598584592Sarutz load_scsi32(&str[24], 1000); 75698584592Sarutz 75798584592Sarutz /* Read Speed */ 75898584592Sarutz load_scsi32(&str[12], (uint32_t)read_speed); 75998584592Sarutz /* Write Speed */ 76098584592Sarutz load_scsi32(&str[20], (uint32_t)write_speed); 76198584592Sarutz 76298584592Sarutz /* issue SET STREAMING command */ 76398584592Sarutz ret = set_streaming(dev->d_fd, str); 76498584592Sarutz free(str); 76598584592Sarutz 76698584592Sarutz return (ret); 76798584592Sarutz } 76898584592Sarutz 76998584592Sarutz /* 77098584592Sarutz * cd speed related functions for drives which support 77198584592Sarutz * Real-Time Streaming Feature. 77298584592Sarutz * 77398584592Sarutz * For the SET operations, the SET STREAMING command needs 77498584592Sarutz * both the Read Speed and the Write Speed. Eg, if 77598584592Sarutz * we're trying to set the Write Speed (SET_WRITE_SPEED), 77698584592Sarutz * then we first need to obtain the current Read Speed. 77798584592Sarutz * That speed is specified along with the chosen_speed (the 77898584592Sarutz * Write Speed in this case) in the SET STREAMING command. 7797c478bd9Sstevel@tonic-gate */ 7807c478bd9Sstevel@tonic-gate int 7817c478bd9Sstevel@tonic-gate rt_streaming_ctrl(cd_device *dev, int cmd, int speed) 7827c478bd9Sstevel@tonic-gate { 78398584592Sarutz int ret = 0; 78498584592Sarutz uint_t rate; 7857c478bd9Sstevel@tonic-gate 78698584592Sarutz switch (cmd) { 78798584592Sarutz case GET_WRITE_SPEED: 78898584592Sarutz rate = cd_speed_get(dev, GET_WRITE_SPEED); 78998584592Sarutz ret = (int)cdrw_bandwidth_to_x(rate); 79098584592Sarutz break; 79198584592Sarutz 79298584592Sarutz case GET_READ_SPEED: 79398584592Sarutz rate = cd_speed_get(dev, GET_READ_SPEED); 79498584592Sarutz ret = (int)cdrw_bandwidth_to_x(rate); 79598584592Sarutz break; 79698584592Sarutz 79798584592Sarutz case SET_READ_SPEED: { 79898584592Sarutz uint_t write_speed = cd_speed_get(dev, GET_WRITE_SPEED); 79998584592Sarutz 80098584592Sarutz /* set Read Speed using SET STREAMING */ 80198584592Sarutz ret = do_set_streaming(dev, 80298584592Sarutz cdrw_x_to_bandwidth(speed), write_speed); 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate /* If rt_speed_ctrl fails for any reason use cd_speed_ctrl */ 8057c478bd9Sstevel@tonic-gate if (ret == 0) { 8067c478bd9Sstevel@tonic-gate if (debug) 8077c478bd9Sstevel@tonic-gate (void) printf(" real time speed control" 8087c478bd9Sstevel@tonic-gate " failed, using CD speed control\n"); 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate dev->d_speed_ctrl = cd_speed_ctrl; 8117c478bd9Sstevel@tonic-gate ret = dev->d_speed_ctrl(dev, cmd, speed); 8127c478bd9Sstevel@tonic-gate } 81398584592Sarutz break; 81498584592Sarutz } 8157c478bd9Sstevel@tonic-gate 81698584592Sarutz case SET_WRITE_SPEED: { 81798584592Sarutz uint_t read_speed = cd_speed_get(dev, GET_READ_SPEED); 81898584592Sarutz 81998584592Sarutz /* set Write Speed using SET STREAMING */ 82098584592Sarutz ret = do_set_streaming(dev, read_speed, 82198584592Sarutz cdrw_x_to_bandwidth(speed)); 82298584592Sarutz 82398584592Sarutz /* If rt_speed_ctrl fails for any reason use cd_speed_ctrl */ 82498584592Sarutz if (ret == 0) { 82598584592Sarutz if (debug) 82698584592Sarutz (void) printf(" real time speed control" 82798584592Sarutz " failed, using CD speed control\n"); 82898584592Sarutz 82998584592Sarutz dev->d_speed_ctrl = cd_speed_ctrl; 83098584592Sarutz ret = dev->d_speed_ctrl(dev, cmd, speed); 83198584592Sarutz } 83298584592Sarutz break; 83398584592Sarutz } 83498584592Sarutz 83598584592Sarutz default: 83698584592Sarutz break; 83798584592Sarutz } 83898584592Sarutz 8397c478bd9Sstevel@tonic-gate return (ret); 8407c478bd9Sstevel@tonic-gate } 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate /* 8437c478bd9Sstevel@tonic-gate * Initialize device for track-at-once mode of writing. All of the data will 8447c478bd9Sstevel@tonic-gate * need to be written to the track without interruption. 8457c478bd9Sstevel@tonic-gate * This initialized TAO by setting page code 5 and speed. 8467c478bd9Sstevel@tonic-gate */ 8477c478bd9Sstevel@tonic-gate void 8487c478bd9Sstevel@tonic-gate write_init(int mode) 8497c478bd9Sstevel@tonic-gate { 8507c478bd9Sstevel@tonic-gate (void) printf(gettext("Initializing device")); 8517c478bd9Sstevel@tonic-gate if (simulation) 8527c478bd9Sstevel@tonic-gate (void) printf(gettext("(Simulation mode)")); 8537c478bd9Sstevel@tonic-gate print_n_flush("..."); 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate get_media_type(target->d_fd); 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate /* DVD- requires DAO mode */ 8587c478bd9Sstevel@tonic-gate if (device_type == DVD_MINUS) { 8597c478bd9Sstevel@tonic-gate write_mode = DAO_MODE; 8607c478bd9Sstevel@tonic-gate } 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate /* For debug, print out device config information */ 8637c478bd9Sstevel@tonic-gate if (debug) { 8647c478bd9Sstevel@tonic-gate int i; 8657c478bd9Sstevel@tonic-gate uchar_t cap[80]; 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate if (get_configuration(target->d_fd, 0, 80, cap)) 8687c478bd9Sstevel@tonic-gate (void) printf("Drive profile = "); 8697c478bd9Sstevel@tonic-gate for (i = 10; i < 70; i += 8) 8707c478bd9Sstevel@tonic-gate (void) printf(" 0x%x", cap[i]); 8717c478bd9Sstevel@tonic-gate (void) printf("\n"); 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate 8747c478bd9Sstevel@tonic-gate /* DVD+ and DVD- have no support for AUDIO, bail out */ 8757c478bd9Sstevel@tonic-gate if ((mode == TRACK_MODE_AUDIO) && (device_type != CD_RW)) { 8767c478bd9Sstevel@tonic-gate err_msg(gettext("Audio mode is only supported for CD media\n")); 8777c478bd9Sstevel@tonic-gate exit(1); 8787c478bd9Sstevel@tonic-gate } 879a2b4fdf6Srameshc if (simulation && 880a2b4fdf6Srameshc check_device(target, CHECK_MEDIA_IS_NOT_BLANK) && 881a2b4fdf6Srameshc !check_device(target, CHECK_MEDIA_IS_NOT_ERASABLE) && 882a2b4fdf6Srameshc device_type != DVD_PLUS_W) { 883a2b4fdf6Srameshc /* 884a2b4fdf6Srameshc * If we were in simulation mode, and media wasn't blank, 885a2b4fdf6Srameshc * but medium was erasable, then cdrw goes to erase the 886a2b4fdf6Srameshc * contents of the media after the simulation writing in order 887a2b4fdf6Srameshc * to cleanup the ghost TOC (see write_fini() calls blank()). 888a2b4fdf6Srameshc * This is bad because it removes existing data if media was 889a2b4fdf6Srameshc * multi-session. Therefore, we no longer allow simulation 890a2b4fdf6Srameshc * writing if such condition is met. we don't blank the DVD+RW 891a2b4fdf6Srameshc * media, so DVD+RWs are fine. 892a2b4fdf6Srameshc */ 893a2b4fdf6Srameshc err_msg(gettext( 894a2b4fdf6Srameshc "Cannot perform simulation for non-blank media\n")); 895a2b4fdf6Srameshc exit(1); 896a2b4fdf6Srameshc } 8977c478bd9Sstevel@tonic-gate 8987c478bd9Sstevel@tonic-gate if (!prepare_for_write(target, mode, simulation, keep_disc_open)) { 8997c478bd9Sstevel@tonic-gate /* l10n_NOTE : 'failed' as in Initializing device...failed */ 9007c478bd9Sstevel@tonic-gate (void) printf(gettext("failed.\n")); 9017c478bd9Sstevel@tonic-gate err_msg(gettext("Cannot initialize device for write\n")); 9027c478bd9Sstevel@tonic-gate exit(1); 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate /* l10n_NOTE : 'done' as in "Initializing device...done" */ 9057c478bd9Sstevel@tonic-gate (void) printf(gettext("done.\n")); 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate /* if speed change option was used (-p) then try to set the speed */ 9087c478bd9Sstevel@tonic-gate if (requested_speed != 0) { 9097c478bd9Sstevel@tonic-gate if (verbose) 9107c478bd9Sstevel@tonic-gate (void) printf(gettext("Trying to set speed to %dX.\n"), 9117c478bd9Sstevel@tonic-gate requested_speed); 9127c478bd9Sstevel@tonic-gate if (target->d_speed_ctrl(target, SET_WRITE_SPEED, 9137c478bd9Sstevel@tonic-gate requested_speed) == 0) { 9147c478bd9Sstevel@tonic-gate err_msg(gettext("Unable to set speed.\n")); 9157c478bd9Sstevel@tonic-gate exit(1); 9167c478bd9Sstevel@tonic-gate } 9177c478bd9Sstevel@tonic-gate if (verbose) { 9187c478bd9Sstevel@tonic-gate int speed; 9197c478bd9Sstevel@tonic-gate speed = target->d_speed_ctrl(target, 9207c478bd9Sstevel@tonic-gate GET_WRITE_SPEED, 0); 9217c478bd9Sstevel@tonic-gate if (speed == requested_speed) { 9227c478bd9Sstevel@tonic-gate (void) printf(gettext("Speed set to %dX.\n"), 9237c478bd9Sstevel@tonic-gate speed); 92498584592Sarutz } else if (speed == 0) { 92598584592Sarutz (void) printf(gettext("Could not obtain " 92698584592Sarutz "current Write Speed.\n")); 9277c478bd9Sstevel@tonic-gate } else { 9287c478bd9Sstevel@tonic-gate (void) printf( 9297c478bd9Sstevel@tonic-gate gettext("Speed set to closest approximation " 9307c478bd9Sstevel@tonic-gate "of %dX allowed by device (%dX).\n"), 9317c478bd9Sstevel@tonic-gate requested_speed, speed); 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate } 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate } 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate void 9387c478bd9Sstevel@tonic-gate write_fini(void) 9397c478bd9Sstevel@tonic-gate { 9407c478bd9Sstevel@tonic-gate print_n_flush(gettext("Finalizing (Can take several minutes)...")); 9417c478bd9Sstevel@tonic-gate /* Some drives don't like this while in test write mode */ 9427c478bd9Sstevel@tonic-gate if (!simulation) { 9437c478bd9Sstevel@tonic-gate if (!finalize(target)) { 9447c478bd9Sstevel@tonic-gate /* 9457c478bd9Sstevel@tonic-gate * It is possible that the drive is busy writing the 9467c478bd9Sstevel@tonic-gate * buffered portion. So do not get upset yet. 9477c478bd9Sstevel@tonic-gate */ 9487c478bd9Sstevel@tonic-gate (void) sleep(10); 9497c478bd9Sstevel@tonic-gate if (!finalize(target)) { 9507c478bd9Sstevel@tonic-gate if (debug) { 9517c478bd9Sstevel@tonic-gate (void) printf("status %x, %x/%x/%x\n", 9527c478bd9Sstevel@tonic-gate uscsi_status, SENSE_KEY(rqbuf), 9537c478bd9Sstevel@tonic-gate ASC(rqbuf), ASCQ(rqbuf)); 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate 956*5d465cc7Sminht /* 957*5d465cc7Sminht * Different vendor drives return different 958*5d465cc7Sminht * sense error info for CLOSE SESSION command. 959*5d465cc7Sminht * The Panasonic drive that we are using is 960*5d465cc7Sminht * one such drive. 961*5d465cc7Sminht */ 962*5d465cc7Sminht if (device_type == DVD_MINUS) { 9637c478bd9Sstevel@tonic-gate if (verbose) { 9647c478bd9Sstevel@tonic-gate (void) printf( 9657c478bd9Sstevel@tonic-gate "skipping finalizing\n"); 9667c478bd9Sstevel@tonic-gate } 9677c478bd9Sstevel@tonic-gate } else { 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate /* l10n_NOTE : 'failed' as in finishing up...failed */ 9707c478bd9Sstevel@tonic-gate (void) printf(gettext("failed.\n")); 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate err_msg(gettext( 9737c478bd9Sstevel@tonic-gate "Could not finalize the disc.\n")); 9747c478bd9Sstevel@tonic-gate exit(1); 9757c478bd9Sstevel@tonic-gate } 9767c478bd9Sstevel@tonic-gate 9777c478bd9Sstevel@tonic-gate 9787c478bd9Sstevel@tonic-gate } 9797c478bd9Sstevel@tonic-gate } 9807c478bd9Sstevel@tonic-gate if (vol_running) { 9817c478bd9Sstevel@tonic-gate (void) eject_media(target); 9827c478bd9Sstevel@tonic-gate } 9837c478bd9Sstevel@tonic-gate } else if (check_device(target, CHECK_MEDIA_IS_NOT_BLANK)) { 9847c478bd9Sstevel@tonic-gate /* 9857c478bd9Sstevel@tonic-gate * Some drives such as the pioneer A04 will retain a 9867c478bd9Sstevel@tonic-gate * ghost TOC after a simulation write is done. The 9877c478bd9Sstevel@tonic-gate * media will actually be blank, but the drive will 9887c478bd9Sstevel@tonic-gate * report a TOC. There is currently no other way to 9897c478bd9Sstevel@tonic-gate * re-initialize the media other than ejecting or 9907c478bd9Sstevel@tonic-gate * to ask the drive to clear the leadout. The laser 9917c478bd9Sstevel@tonic-gate * is currently off so nothing is written to the 9927c478bd9Sstevel@tonic-gate * media (on a good behaving drive). 9937c478bd9Sstevel@tonic-gate * NOTE that a device reset does not work to make 9947c478bd9Sstevel@tonic-gate * the drive re-initialize the media. 9957c478bd9Sstevel@tonic-gate */ 9967c478bd9Sstevel@tonic-gate 997a2b4fdf6Srameshc blanking_type = "clear_ghost"; 9987c478bd9Sstevel@tonic-gate blank(); 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate } 10017c478bd9Sstevel@tonic-gate /* l10n_NOTE : 'done' as in "Finishing up...done" */ 10027c478bd9Sstevel@tonic-gate (void) printf(gettext("done.\n")); 10037c478bd9Sstevel@tonic-gate } 1004