1 /*****************************************************************************
2 * intf_eject.c: CD/DVD-ROM ejection handling functions
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: intf_eject.c,v 1.8 2002/04/04 05:08:05 sam Exp $
7 * Author: Julien Blache <jb@technologeek.org> for the Linux part
8 * with code taken from the Linux "eject" command
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include <videolan/vlc.h>
45 # include <linux/version.h>
46 /* handy macro found in 2.1 kernels, but not in older ones */
47 # ifndef KERNEL_VERSION
48 # define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
51 # include <sys/types.h>
52 # include <sys/stat.h>
53 # include <sys/ioctl.h>
55 # include <sys/ioctl.h>
56 # include <sys/mount.h>
58 # include <linux/cdrom.h>
59 # if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
60 # include <linux/ucdrom.h>
63 # ifdef HAVE_SCSI_SCSI_IOCTL_H
64 # include <scsi/scsi.h>
66 # include <scsi/scsi_ioctl.h>
70 /*****************************************************************************
72 *****************************************************************************/
73 #if defined(SYS_LINUX) && defined(HAVE_SCSI_SCSI_IOCTL_H)
74 static int EjectSCSI ( int i_fd );
77 /*****************************************************************************
78 * intf_Eject: eject the CDRom
79 *****************************************************************************
80 * returns 0 on success
81 * returns 1 on failure
82 * returns -1 if not implemented
83 *****************************************************************************/
84 int intf_Eject( const char *psz_device )
95 * The only way to cleanly unmount the disc under MacOS X
96 * is to use the 'disktool' command line utility. It uses
97 * the non-public Disk Arbitration API, which can not be
98 * used by Cocoa or Carbon applications.
101 if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
102 strlen( psz_disk ) > 4 )
104 #define EJECT_CMD "disktool -e %s 0"
105 snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
108 if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
110 char psz_result[0x200];
111 i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
113 if( i_ret == 0 && ferror( p_eject ) != 0 )
121 psz_result[ i_ret ] = 0;
123 if( strstr( psz_result, "Disk Ejected" ) != NULL )
134 /* This code could be extended to support CD/DVD-ROM chargers */
136 i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
140 intf_ErrMsg( "intf error: couldn't open device %s", psz_device );
145 /* Try a simple ATAPI eject */
146 i_ret = ioctl( i_fd, CDROMEJECT, 0 );
148 #ifdef HAVE_SCSI_SCSI_IOCTL_H
151 i_ret = EjectSCSI( i_fd );
157 intf_ErrMsg( "intf error: couldn't eject %s", psz_device );
160 #elif defined (HAVE_DVD_H)
161 i_ret = ioctl( i_fd, CDROMEJECT, 0 );
164 intf_ErrMsg( "intf error: CD-Rom ejection unsupported on this platform" );
173 /* The following functions are local */
175 #if defined(SYS_LINUX) && defined(HAVE_SCSI_SCSI_IOCTL_H)
176 /*****************************************************************************
177 * Eject using SCSI commands. Return 0 if successful
178 *****************************************************************************/
179 static int EjectSCSI( int i_fd )
192 scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
198 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
206 scsi_cmd.cmd[0] = START_STOP;
212 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
220 scsi_cmd.cmd[0] = START_STOP;
226 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
232 /* Force kernel to reread partition table when new disc inserted */
233 i_status = ioctl( i_fd, BLKRRPART );