1 /*****************************************************************************
2 * intf_eject.c: CD/DVD-ROM ejection handling functions
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Julien Blache <jb@technologeek.org> for the Linux part
8 * with code taken from the Linux "eject" command
9 * Jon Lech Johansen <jon-vl@nanocrew.net> for Darwin
10 * Gildas Bazin <gbazin@netcourrier.com> for Win32
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
29 * This file contain functions to eject CD and DVD drives
50 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
51 # include <linux/version.h>
52 /* handy macro found in 2.1 kernels, but not in older ones */
53 # ifndef KERNEL_VERSION
54 # define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
57 # include <sys/types.h>
58 # include <sys/stat.h>
59 # include <sys/ioctl.h>
61 # include <sys/ioctl.h>
62 # include <sys/mount.h>
64 # include <linux/cdrom.h>
65 # if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
66 # include <linux/ucdrom.h>
69 # include <scsi/scsi.h>
71 # include <scsi/scsi_ioctl.h>
74 #if defined( WIN32 ) && !defined( UNDER_CE )
75 # include <mmsystem.h>
78 #include <vlc_interface.h>
80 /*****************************************************************************
82 *****************************************************************************/
83 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
84 static int EjectSCSI ( int i_fd );
87 /*****************************************************************************
88 * intf_Eject: eject the CDRom
89 *****************************************************************************
90 * returns 0 on success
91 * returns 1 on failure
92 * returns -1 if not implemented
93 *****************************************************************************/
95 * \brief Ejects the CD /DVD
96 * \ingroup vlc_interface
97 * \param p_this the calling vlc_object_t
98 * \param psz_device the CD/DVD to eject
99 * \return 0 on success, 1 on failure, -1 if not implemented
101 int __intf_Eject( vlc_object_t *p_this, const char *psz_device )
104 int i_ret = VLC_SUCCESS;
112 * The only way to cleanly unmount the disc under MacOS X
113 * is to use the 'disktool' command line utility. It uses
114 * the non-public Disk Arbitration API, which can not be
115 * used by Cocoa or Carbon applications.
118 if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
119 strlen( psz_disk ) > 4 )
121 #define EJECT_CMD "/usr/sbin/disktool -e %s 0"
122 snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
125 if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
127 char psz_result[0x200];
128 i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
130 if( i_ret == 0 && ferror( p_eject ) != 0 )
138 psz_result[ i_ret ] = 0;
140 if( strstr( psz_result, "Disk Ejected" ) != NULL )
149 #elif defined(UNDER_CE)
150 msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
159 memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
160 op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
162 strcpy( psz_drive, "X:" );
163 psz_drive[0] = psz_device[0];
164 op.lpstrElementName = psz_drive;
166 /* Set the flags for the device type */
167 i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
168 MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
170 if( !mciSendCommand( 0, MCI_OPEN, i_flags, (unsigned long)&op ) )
172 st.dwItem = MCI_STATUS_READY;
174 i_ret = mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 );
175 /* Release access to the device */
176 mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
178 else i_ret = VLC_EGENERIC;
185 /* This code could be extended to support CD/DVD-ROM chargers */
187 i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
191 msg_Err( p_this, "could not open device %s", psz_device );
195 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
196 /* Try a simple ATAPI eject */
197 i_ret = ioctl( i_fd, CDROMEJECT, 0 );
201 i_ret = EjectSCSI( i_fd );
206 msg_Err( p_this, "could not eject %s", psz_device );
209 #elif defined (HAVE_DVD_H)
210 i_ret = ioctl( i_fd, CDROMEJECT, 0 );
213 msg_Warn( p_this, "CD-ROM ejection unsupported on this platform" );
223 /* The following functions are local */
225 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
226 /*****************************************************************************
227 * Eject using SCSI commands. Return 0 if successful
228 *****************************************************************************/
230 * \brief Ejects the CD /DVD using SCSI commands
231 * \ingroup vlc_interface
232 * This function is local
233 * \param i_fd a device nummber
234 * \return 0 on success, VLC_EGENERIC on failure
236 static int EjectSCSI( int i_fd )
249 scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
255 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
263 scsi_cmd.cmd[0] = START_STOP;
269 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
277 scsi_cmd.cmd[0] = START_STOP;
283 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
289 /* Force kernel to reread partition table when new disc inserted */
290 i_status = ioctl( i_fd, BLKRRPART );