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