1 /*
2  * This file is part of WorkMan, the civilized CD player library
3  * Copyright (C) 1991-1997 by Steven Grimm <koreth@midwinter.com>
4  * Copyright (C) by Dirk Försterling <milliByte@DeathsDoor.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  *
21  * HP/UX-specific drive control routines.
22  */
23 
24 #if defined(hpux) || defined(__hpux)
25 
26 #include <errno.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <ustat.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 
35 #include "include/wm_config.h"
36 
37 /*
38  * this is for glibc 2.x which the ust structure in
39  * ustat.h not stat.h
40  */
41 #ifdef __GLIBC__
42 #include <sys/ustat.h>
43 #endif
44 
45 #include <sys/time.h>
46 #include <sys/scsi.h>
47 
48 #include "include/wm_struct.h"
49 #include "include/wm_helpers.h"
50 #include "include/wm_cdtext.h"
51 
52 #define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
53 
54 void *malloc();
55 char *strchr();
56 
57 int	min_volume = 0;
58 int	max_volume = 255;
59 
60 /*--------------------------------------------------------*
61  * Initialize the drive.  A no-op for the generic driver.
62  *--------------------------------------------------------*/
63 int
gen_init(struct wm_drive * d)64 gen_init( struct wm_drive *d )
65 {
66   return 0;
67 } /* gen_init() */
68 
69 
70 /*-------------------------------------------------------------*
71  * Open the CD and figure out which kind of drive is attached.
72  *-------------------------------------------------------------*/
73 int
gen_open(struct wm_drive * d)74 gen_open( struct wm_drive *d )
75 {
76 	int flag = 1;
77 
78 	if (d->fd >= 0) {	/* Device already open? */
79 		wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "gen_open(): [device is open (fd=%d)]\n", d->fd);
80 		return 0;
81     }
82 
83 
84   	d->fd = open(d->cd_device, O_RDWR);
85   	if (d->fd < 0) {
86 		if (errno == EACCES) {
87           	return -EACCES;
88 		} else if (errno != EINTR) {
89           	return -6;
90 		}
91 
92       	/* No CD in drive. */
93       	return 1;
94     }
95 
96   	/* Prepare the device to receive raw SCSI commands. */
97   	if (ioctl(d->fd, SIOC_CMD_MODE, &flag) < 0) {
98       fprintf(stderr, "%s: SIOC_CMD_MODE: true: %s\n",
99 	      d->cd_device, strerror(errno));
100       /*exit(1);*/
101     }
102 
103   /* Default drive is the HP one, which might not respond to INQUIRY */
104   strcpy(d->vendor, "TOSHIBA");
105   strcpy(d->model, "XM-3301");
106   d->rev[0] = '\0';
107 
108   return 0;
109 } /* gen_open() */
110 
111 /*----------------------------------*
112  * Send a SCSI command out the bus.
113  *----------------------------------*/
114 int
gen_scsi(struct wm_drive * d,unsigned char * cdb,int cdblen,void * retbuf,int retbuflen,int getreply)115 gen_scsi( struct wm_drive *d, unsigned char *cdb, int cdblen,
116 	 void *retbuf, int retbuflen, int getreply )
117 {
118 #ifdef SIOC_IO
119 	struct sctl_io cmd;
120 
121 	memset(&cmd, 0, sizeof(cmd));
122 	cmd.cdb_length = cdblen;
123 	cmd.data = retbuf;
124 	cmd.data_length = retbuflen;
125 	cmd.max_msecs = 1000;
126 	cmd.flags = getreply ? SCTL_READ : 0;
127 	memcpy(cmd.cdb, cdb, cdblen);
128 
129 	return ioctl(d->fd, SIOC_IO, &cmd);
130 #else
131 	/* this code, for pre-9.0, is BROKEN!  ugh. */
132 	char reply_buf[12];
133 	struct scsi_cmd_parms cmd;
134 
135 	memset(&cmd, 0, sizeof(cmd));
136 	cmd.clock_ticks = 500;
137 	cmd.cmd_mode = 1;
138 	cmd.cmd_type = cdblen;
139 	memcpy(cmd.command, cdb, cdblen);
140 	if (ioctl(d->fd, SIOC_SET_CMD, &cmd) < 0)
141 		return -1;
142 
143 	if (! retbuf || ! retbuflen)
144     	read(d->fd, reply_buf, sizeof(reply_buf));
145   	else if (getreply) {
146 		if (read(d->fd, retbuf, retbuflen) < 0)
147 			return -1;
148     } else if (write(d->fd, retbuf, retbuflen) < 0)
149 		return -1;
150 
151 	return 0;
152 #endif
153 } /* gen_scsi() */
154 
155 int
gen_close(struct wm_drive * d)156 gen_close( struct wm_drive *d )
157 {
158 	if(d->fd != -1) {
159 		wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closing the device\n");
160 		close(d->fd);
161 		d->fd = -1;
162 	}
163 	return 0;
164 }
165 
166 /*--------------------------------------------------------------------------*
167  * Get the current status of the drive: the current play mode, the absolute
168  * position from start of disc (in frames), and the current track and index
169  * numbers if the CD is playing or paused.
170  *--------------------------------------------------------------------------*/
171 int
gen_get_drive_status(struct wm_drive * d,int oldmode,int * mode,int * pos,int * track,int * index)172 gen_get_drive_status( struct wm_drive *d, int oldmode, int *mode,
173                       int *pos, int *track, int *index )
174 {
175 	return wm_scsi2_get_drive_status(d, oldmode, mode, pos, track, index);
176 } /* gen_get_drive_status() */
177 
178 /*-------------------------------------*
179  * Get the number of tracks on the CD.
180  *-------------------------------------*/
181 int
gen_get_trackcount(struct wm_drive * d,int * tracks)182 gen_get_trackcount(struct wm_drive *d, int *tracks )
183 {
184 	return wm_scsi2_get_trackcount(d, tracks);
185 } /* gen_get_trackcount() */
186 
187 /*---------------------------------------------------------*
188  * Get the start time and mode (data or audio) of a track.
189  *---------------------------------------------------------*/
190 int
gen_get_trackinfo(struct wm_drive * d,int * track,int * data,int * startframe)191 gen_get_trackinfo( struct wm_drive *d, int *track, int *data, int *startframe)
192 {
193 	return wm_scsi2_get_trackinfo(d, track, data, startframe);
194 } /* gen_get_trackinfo() */
195 
196 /*-------------------------------------*
197  * Get the number of frames on the CD.
198  *-------------------------------------*/
gen_get_cdlen(struct wm_drive * d,int * frames)199 int gen_get_cdlen(struct wm_drive *d, int *frames)
200 {
201 	return wm_scsi2_get_cdlen(d, frames);
202 } /* gen_get_cdlen() */
203 
204 /*------------------------------------------------------------*
205  * Play the CD from one position to another (both in frames.)
206  *------------------------------------------------------------*/
207 int
gen_play(struct wm_drive * d,int start,int end)208 gen_play( struct wm_drive *d, int start, int end )
209 {
210 	return wm_scsi2_play(d, start, end);
211 } /* gen_play() */
212 
213 /*---------------*
214  * Pause the CD.
215  *---------------*/
216 int
gen_pause(struct wm_drive * d)217 gen_pause( struct wm_drive *d )
218 {
219 	return wm_scsi2_pause(d);
220 } /* gen_pause() */
221 
222 /*-------------------------------------------------*
223  * Resume playing the CD (assuming it was paused.)
224  *-------------------------------------------------*/
225 int
gen_resume(struct wm_drive * d)226 gen_resume( struct wm_drive *d )
227 {
228 	return wm_scsi2_resume(d);
229 } /* gen_resume() */
230 
231 /*--------------*
232  * Stop the CD.
233  *--------------*/
234 int
gen_stop(struct wm_drive * d)235 gen_stop( struct wm_drive *d )
236 {
237 	return wm_scsi2_stop(d);
238 } /* gen_stop() */
239 
240 
241 /*----------------------------------------*
242  * Eject the current CD, if there is one.
243  *----------------------------------------*/
244 int
gen_eject(struct wm_drive * d)245 gen_eject( struct wm_drive *d )
246 {
247 	struct stat stbuf;
248 	struct ustat ust;
249 
250 	if (fstat(d->fd, &stbuf) != 0)
251     	return -2;
252 
253 	/* Is this a mounted filesystem? */
254 	if (ustat(stbuf.st_rdev, &ust) == 0)
255 		return -3;
256 
257 	return wm_scsi2_eject(d);
258 } /* gen_eject() */
259 
260 /*----------------------------------------*
261  * Close the CD tray
262  *----------------------------------------*/
263 int
gen_closetray(struct wm_drive * d)264 gen_closetray(struct wm_drive *d)
265 {
266 	return wm_scsi2_closetray(d);
267 } /* gen_closetray() */
268 
269 
270 /*---------------------------------------------------------------------*
271  * Set the volume level for the left and right channels.  Their values
272  * range from 0 to 100.
273  *---------------------------------------------------------------------*/
274 int
gen_set_volume(struct wm_drive * d,int left,int right)275 gen_set_volume( struct wm_drive *d, int left, int right )
276 {
277 	return wm_scsi2_set_volume(d, left, right);
278 } /* gen_set_volume() */
279 
280 /*---------------------------------------------------------------------*
281  * Read the initial volume from the drive, if available.  Each channel
282  * ranges from 0 to 100, with -1 indicating data not available.
283  *---------------------------------------------------------------------*/
284 int
gen_get_volume(struct wm_drive * d,int * left,int * right)285 gen_get_volume( struct wm_drive *d, int *left, int *right )
286 {
287 	return wm_scsi2_get_volume(d, left, right);
288 } /* gen_get_volume() */
289 
290 #endif
291 
292 
293