]> git.sesse.net Git - vlc/blob - src/interface/intf_eject.c
* BSD compilation fix suggested by Steven M. Schultz.
[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.3 2002/01/11 03:07:36 sam 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 <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29
30 #include <videolan/vlc.h>
31
32 #ifdef HAVE_FCNTL_H
33 #   include <fcntl.h>
34 #endif
35
36 #ifdef HAVE_DVD_H
37 #   include <dvd.h>
38 #endif
39
40 #ifdef SYS_LINUX
41 #   include <linux/version.h>
42     /* handy macro found in 2.1 kernels, but not in older ones */
43 #   ifndef KERNEL_VERSION
44 #       define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
45 #   endif
46
47 #   include <sys/types.h>
48 #   include <sys/stat.h>
49 #   include <sys/ioctl.h>
50
51 #   include <sys/ioctl.h>
52 #   include <linux/cdrom.h>
53 #   if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
54 #       include <linux/ucdrom.h>
55 #   endif
56
57 #   include <sys/mount.h>
58 #   include <scsi/scsi.h>
59 #   include <scsi/sg.h>
60 #   include <scsi/scsi_ioctl.h>
61 #endif
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 #ifdef SYS_LINUX
67 static int EjectSCSI ( int i_fd );
68 #endif
69
70 /*****************************************************************************
71  * intf_Eject: eject the CDRom
72  *****************************************************************************
73  * returns 0 on success
74  * returns 1 on failure
75  * returns -1 if not implemented
76  *****************************************************************************/
77 int intf_Eject( const char *psz_device )
78 {
79     int i_ret;
80
81     /* This code could be extended to support CD/DVD-ROM chargers */
82     int i_fd = 0;
83
84     i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
85    
86     if( i_fd == -1 )
87     {
88         intf_ErrMsg( "intf error: couldn't open device %s", psz_device );
89         return 1;
90     }
91
92 #ifdef SYS_LINUX
93     /* Try a simple ATAPI eject */
94     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
95
96     if( i_ret != 0 )
97     {
98         i_ret = EjectSCSI( i_fd );
99     }
100
101     if( i_ret != 0 )
102     {
103         intf_ErrMsg( "intf error: couldn't eject %s", psz_device );
104     }
105
106 #elif defined (HAVE_DVD_H)
107     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
108
109 #else
110     intf_ErrMsg( "intf error: CD-Rom ejection unsupported on this platform" );
111     i_ret = -1;
112
113 #endif
114     close( i_fd );
115
116     return i_ret;
117 }
118
119 /* The following functions are local */
120
121 #ifdef SYS_LINUX
122 /*****************************************************************************
123  * Eject using SCSI commands. Return 0 if successful
124  *****************************************************************************/
125 static int EjectSCSI( int i_fd )
126 {
127     int i_status;
128
129     struct sdata
130     {
131         int  inlen;
132         int  outlen;
133         char cmd[256];
134     } scsi_cmd;
135
136     scsi_cmd.inlen  = 0;
137     scsi_cmd.outlen = 0;
138     scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
139     scsi_cmd.cmd[1] = 0;
140     scsi_cmd.cmd[2] = 0;
141     scsi_cmd.cmd[3] = 0;
142     scsi_cmd.cmd[4] = 0;
143     scsi_cmd.cmd[5] = 0;
144     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
145     if( i_status != 0 )
146     {
147         return 1;
148     }
149
150     scsi_cmd.inlen  = 0;
151     scsi_cmd.outlen = 0;
152     scsi_cmd.cmd[0] = START_STOP;
153     scsi_cmd.cmd[1] = 0;
154     scsi_cmd.cmd[2] = 0;
155     scsi_cmd.cmd[3] = 0;
156     scsi_cmd.cmd[4] = 1;
157     scsi_cmd.cmd[5] = 0;
158     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
159     if( i_status != 0 )
160     {
161         return 1;
162     }
163   
164     scsi_cmd.inlen  = 0;
165     scsi_cmd.outlen = 0;
166     scsi_cmd.cmd[0] = START_STOP;
167     scsi_cmd.cmd[1] = 0;
168     scsi_cmd.cmd[2] = 0;
169     scsi_cmd.cmd[3] = 0;
170     scsi_cmd.cmd[4] = 2;
171     scsi_cmd.cmd[5] = 0;
172     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
173     if( i_status != 0 )
174     {
175         return 1;
176     }
177   
178     /* Force kernel to reread partition table when new disc inserted */
179     i_status = ioctl( i_fd, BLKRRPART );
180   
181     return i_status;
182 }
183 #endif
184