]> git.sesse.net Git - vlc/blob - src/interface/intf_eject.c
* Fix for iPAQ familiar Linux (untested)
[vlc] / src / interface / intf_eject.c
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.6 2002/04/03 23:24:42 massiot Exp $
6  *
7  * Author: Julien Blache <jb@technologeek.org> for the Linux part
8  *               with code taken from the Linux "eject" command
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include <videolan/vlc.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #ifdef HAVE_UNISTD_H
31 #    include <unistd.h>
32 #endif
33
34 #include <string.h>
35
36 #ifdef HAVE_FCNTL_H
37 #   include <fcntl.h>
38 #endif
39
40 #ifdef HAVE_DVD_H
41 #   include <dvd.h>
42 #endif
43
44 #ifdef SYS_LINUX
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))
49 #   endif
50
51 #   include <sys/types.h>
52 #   include <sys/stat.h>
53 #   include <sys/ioctl.h>
54
55 #   include <sys/ioctl.h>
56 #   include <linux/cdrom.h>
57 #   if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
58 #       include <linux/ucdrom.h>
59 #   endif
60
61 #   include <sys/mount.h>
62 #   include <scsi/scsi.h>
63 #   include <scsi/sg.h>
64 #   ifdef HAVE_SCSI_SCSI_IOCTL_H
65 #      include <scsi/scsi_ioctl.h>
66 #   endif
67 #endif
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 #if defined(SYS_LINUX) && defined(HAVE_SCSI_SCSI_IOCTL_H)
73 static int EjectSCSI ( int i_fd );
74 #endif
75
76 /*****************************************************************************
77  * intf_Eject: eject the CDRom
78  *****************************************************************************
79  * returns 0 on success
80  * returns 1 on failure
81  * returns -1 if not implemented
82  *****************************************************************************/
83 int intf_Eject( const char *psz_device )
84 {
85     int i_fd;
86     int i_ret;
87
88 #ifdef SYS_DARWIN
89     FILE *p_eject;
90     char *psz_disk;
91     char sz_cmd[32];
92
93     /*
94      * The only way to cleanly unmount the disc under MacOS X
95      * is to use the 'disktool' command line utility. It uses
96      * the non-public Disk Arbitration API, which can not be
97      * used by Cocoa or Carbon applications. 
98      */
99
100     if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
101         strlen( psz_disk ) > 4 )
102     {
103 #define EJECT_CMD "disktool -e %s 0"
104         snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
105 #undef EJECT_CMD
106
107         if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
108         {
109             char psz_result[0x200];
110             i_ret = fread( psz_result, 1, sizeof(psz_result), p_eject );
111             pclose( p_eject );
112
113             if( i_ret == 0 && ferror( p_eject ) != 0 )
114             {
115                 return 1;
116             }
117
118             if( strstr( psz_result, "Disk Ejected" ) != NULL )
119             {
120                 return 0;
121             }
122         }
123     }
124
125     return 1;
126
127 #endif
128
129     /* This code could be extended to support CD/DVD-ROM chargers */
130
131     i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
132    
133     if( i_fd == -1 )
134     {
135         intf_ErrMsg( "intf error: couldn't open device %s", psz_device );
136         return 1;
137     }
138
139 #ifdef SYS_LINUX
140     /* Try a simple ATAPI eject */
141     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
142
143 #ifdef HAVE_SCSI_SCSI_IOCTL_H
144     if( i_ret != 0 )
145     {
146         i_ret = EjectSCSI( i_fd );
147     }
148 #endif
149
150     if( i_ret != 0 )
151     {
152         intf_ErrMsg( "intf error: couldn't eject %s", psz_device );
153     }
154
155 #elif defined (HAVE_DVD_H)
156     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
157
158 #else
159     intf_ErrMsg( "intf error: CD-Rom ejection unsupported on this platform" );
160     i_ret = -1;
161
162 #endif
163     close( i_fd );
164
165     return i_ret;
166 }
167
168 /* The following functions are local */
169
170 #if defined(SYS_LINUX) && defined(HAVE_SCSI_SCSI_IOCTL_H)
171 /*****************************************************************************
172  * Eject using SCSI commands. Return 0 if successful
173  *****************************************************************************/
174 static int EjectSCSI( int i_fd )
175 {
176     int i_status;
177
178     struct sdata
179     {
180         int  inlen;
181         int  outlen;
182         char cmd[256];
183     } scsi_cmd;
184
185     scsi_cmd.inlen  = 0;
186     scsi_cmd.outlen = 0;
187     scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
188     scsi_cmd.cmd[1] = 0;
189     scsi_cmd.cmd[2] = 0;
190     scsi_cmd.cmd[3] = 0;
191     scsi_cmd.cmd[4] = 0;
192     scsi_cmd.cmd[5] = 0;
193     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
194     if( i_status != 0 )
195     {
196         return 1;
197     }
198
199     scsi_cmd.inlen  = 0;
200     scsi_cmd.outlen = 0;
201     scsi_cmd.cmd[0] = START_STOP;
202     scsi_cmd.cmd[1] = 0;
203     scsi_cmd.cmd[2] = 0;
204     scsi_cmd.cmd[3] = 0;
205     scsi_cmd.cmd[4] = 1;
206     scsi_cmd.cmd[5] = 0;
207     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
208     if( i_status != 0 )
209     {
210         return 1;
211     }
212   
213     scsi_cmd.inlen  = 0;
214     scsi_cmd.outlen = 0;
215     scsi_cmd.cmd[0] = START_STOP;
216     scsi_cmd.cmd[1] = 0;
217     scsi_cmd.cmd[2] = 0;
218     scsi_cmd.cmd[3] = 0;
219     scsi_cmd.cmd[4] = 2;
220     scsi_cmd.cmd[5] = 0;
221     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
222     if( i_status != 0 )
223     {
224         return 1;
225     }
226   
227     /* Force kernel to reread partition table when new disc inserted */
228     i_status = ioctl( i_fd, BLKRRPART );
229   
230     return i_status;
231 }
232 #endif
233