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.1 2002/01/09 02:01:14 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 *****************************************************************************/
30 #include <videolan/vlc.h>
34 /* This code could be extended to support CD/DVD-ROM chargers */
35 # include <linux/version.h>
36 /* handy macro found in 2.1 kernels, but not in older ones */
37 # ifndef KERNEL_VERSION
38 # define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
41 # include <sys/types.h>
42 # include <sys/stat.h>
43 # include <sys/ioctl.h>
46 # include <sys/ioctl.h>
47 # include <linux/cdrom.h>
48 # if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
49 # include <linux/ucdrom.h>
52 # include <sys/mount.h>
53 # include <scsi/scsi.h>
55 # include <scsi/scsi_ioctl.h>
59 static int EjectCdrom( int i_fd );
60 static int EjectScsi ( int i_fd );
63 * Eject using CDROMEJECT ioctl. Return 0 if successful
65 static int EjectCdrom( int i_fd )
69 i_status = ioctl( i_fd, CDROMEJECT );
76 * Eject using SCSI commands. Return 0 if successful
78 static int EjectScsi( int i_fd )
91 scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
97 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
105 scsi_cmd.cmd[0] = START_STOP;
111 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
119 scsi_cmd.cmd[0] = START_STOP;
125 i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
131 /* Force kernel to reread partition table when new disc inserted */
132 i_status = ioctl( i_fd, BLKRRPART );
138 * returns 0 on success
139 * returns 1 on failure
140 * returns -1 if not implemented
142 * Modify eject_disc() prototype as needed for portability
145 int intf_Eject( const char *psz_device )
152 i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
156 intf_ErrMsg( "intf error: couldn't open device %s", psz_device );
160 if( EjectCdrom(i_fd) == 0 )
164 else if( EjectScsi(i_fd) == 0 )
170 intf_ErrMsg( "intf error: couldn't eject %s", psz_device );