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.18 2002/11/13 20:51:05 sam Exp $
7 * Author: 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 * Xavier Marchesini <xav@alarue.net> 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
46 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
47 # include <linux/version.h>
48 /* handy macro found in 2.1 kernels, but not in older ones */
49 # ifndef KERNEL_VERSION
50 # define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
53 # include <sys/types.h>
54 # include <sys/stat.h>
55 # include <sys/ioctl.h>
57 # include <sys/ioctl.h>
58 # include <sys/mount.h>
60 # include <linux/cdrom.h>
61 # if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
62 # include <linux/ucdrom.h>
65 # include <scsi/scsi.h>
67 # include <scsi/scsi_ioctl.h>
70 #if defined( WIN32 ) && !defined( UNDER_CE )
73 # include <winioctl.h>
77 /* define the structures to eject under Win95/98/Me */
79 #if !defined (VWIN32_DIOC_DOS_IOCTL)
80 #define VWIN32_DIOC_DOS_IOCTL 1
82 typedef struct _DIOC_REGISTERS {
90 } DIOC_REGISTERS, *PDIOC_REGISTERS;
91 #endif /* VWIN32_DIOC_DOS_IOCTL */
96 /*****************************************************************************
98 *****************************************************************************/
99 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
100 static int EjectSCSI ( int i_fd );
103 /*****************************************************************************
104 * intf_Eject: eject the CDRom
105 *****************************************************************************
106 * returns 0 on success
107 * returns 1 on failure
108 * returns -1 if not implemented
109 *****************************************************************************/
110 int __intf_Eject( vlc_object_t *p_this, const char *psz_device )
112 int i_ret = VLC_SUCCESS;
120 * The only way to cleanly unmount the disc under MacOS X
121 * is to use the 'disktool' command line utility. It uses
122 * the non-public Disk Arbitration API, which can not be
123 * used by Cocoa or Carbon applications.
126 if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
127 strlen( psz_disk ) > 4 )
129 #define EJECT_CMD "/usr/sbin/disktool -e %s 0"
130 snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
133 if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
135 char psz_result[0x200];
136 i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
138 if( i_ret == 0 && ferror( p_eject ) != 0 )
146 psz_result[ i_ret ] = 0;
148 if( strstr( psz_result, "Disk Ejected" ) != NULL )
157 #elif defined(UNDER_CE)
158 msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
163 TCHAR psz_drive_id[8] ;
164 DWORD dw_access_flags = GENERIC_READ ;
166 LPTSTR psz_volume_format = TEXT("\\\\.\\%s") ;
168 DIOC_REGISTERS regs = {0} ;
170 /* Win2K ejection code */
171 if ( GetVersion() < 0x80000000 )
173 wsprintf(psz_drive_id, psz_volume_format, psz_device) ;
175 msg_Dbg( p_this, "ejecting drive %s", psz_drive_id );
177 /* Create the file handle */
178 h_drive = CreateFile( psz_drive_id,
180 FILE_SHARE_READ | FILE_SHARE_WRITE,
186 if (h_drive == INVALID_HANDLE_VALUE )
188 msg_Err( p_this, "could not create handle for device %s",
192 i_ret = DeviceIoControl ( h_drive,
193 IOCTL_STORAGE_EJECT_MEDIA,
200 else /* Win95/98/ME */
202 /* Create the handle to VWIN32 */
203 h_drive = CreateFile ("\\\\.\\vwin32", 0, 0, NULL, 0,
204 FILE_FLAG_DELETE_ON_CLOSE, NULL ) ;
206 /* Convert logical disk name to DOS-like disk name */
207 by_drive = (toupper (*psz_device) - 'A') + 1;
209 /* Let's eject now : Int 21H function 440DH minor code 49h*/
210 regs.reg_EAX = 0x440D ;
211 regs.reg_EBX = by_drive ;
212 regs.reg_ECX = MAKEWORD(0x49 , 0x08) ; // minor code
214 i_ret = DeviceIoControl (h_drive, VWIN32_DIOC_DOS_IOCTL,
215 ®s, sizeof(regs), ®s, sizeof(regs),
218 CloseHandle (h_drive) ;
226 /* This code could be extended to support CD/DVD-ROM chargers */
228 i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
232 msg_Err( p_this, "could not open device %s", psz_device );
236 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
237 /* Try a simple ATAPI eject */
238 i_ret = ioctl( i_fd, CDROMEJECT, 0 );
242 i_ret = EjectSCSI( i_fd );
247 msg_Err( p_this, "could not eject %s", psz_device );
250 #elif defined (HAVE_DVD_H)
251 i_ret = ioctl( i_fd, CDROMEJECT, 0 );
254 msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
264 /* The following functions are local */
266 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
267 /*****************************************************************************
268 * Eject using SCSI commands. Return 0 if successful
269 *****************************************************************************/
270 static int EjectSCSI( int i_fd )
283 scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
289 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
297 scsi_cmd.cmd[0] = START_STOP;
303 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
311 scsi_cmd.cmd[0] = START_STOP;
317 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
323 /* Force kernel to reread partition table when new disc inserted */
324 i_status = ioctl( i_fd, BLKRRPART );