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
36 #include <vlc_common.h>
38 #include <vlc_interface.h>
40 #if defined( WIN32 ) && !defined( UNDER_CE )
41 # include <mmsystem.h>
42 #elif defined(__linux__)
43 # include <sys/types.h>
46 # include <sys/ioctl.h>
47 # include <sys/mount.h>
49 # include <linux/cdrom.h>
51 # include <scsi/scsi.h>
53 # include <scsi/scsi_ioctl.h>
54 #elif defined (HAVE_DVD_H)
60 #if defined(__linux__)
62 * \brief Ejects the CD /DVD using SCSI commands
63 * \ingroup vlc_interface
64 * This function is local
65 * \param i_fd a device nummber
66 * \return 0 on success, VLC_EGENERIC on failure
68 static int EjectSCSI( int i_fd )
79 scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
85 if( ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd ) < 0 )
90 scsi_cmd.cmd[0] = START_STOP;
96 if( ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd ) < 0 )
101 scsi_cmd.cmd[0] = START_STOP;
107 if( ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd ) < 0 )
110 /* Force kernel to reread partition table when new disc inserted */
111 ioctl( i_fd, BLKRRPART );
118 * \brief Ejects the CD /DVD
119 * \ingroup vlc_interface
120 * \param p_this the calling vlc_object_t
121 * \param psz_device the CD/DVD to eject
122 * \return 0 on success, 1 on failure, -1 if not implemented
124 int intf_Eject( vlc_object_t *p_this, const char *psz_device )
133 memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
134 op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
136 strcpy( psz_drive, "X:" );
137 psz_drive[0] = psz_device[0];
138 op.lpstrElementName = psz_drive;
140 /* Set the flags for the device type */
141 i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
142 MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
144 if( mciSendCommand( 0, MCI_OPEN, i_flags, (uintptr_t)&op ) )
148 mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 );
149 /* Release access to the device */
150 mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
154 #elif defined (__linux__) || defined (HAVE_DVD_H)
155 /* This code could be extended to support CD/DVD-ROM chargers */
156 int fd = vlc_open( psz_device, O_RDONLY | O_NONBLOCK );
159 msg_Err( p_this, "could not open device %s", psz_device );
163 # if defined(__linux__)
164 /* Try a simple ATAPI eject */
165 if( ioctl( fd, CDROMEJECT, 0 ) < 0
168 if( ioctl( fd, CDROMEJECT, 0 ) < 0 )
171 msg_Err( p_this, "could not eject %s", psz_device );
178 msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );