2 * $Id: plat_bsd386.c,v 1.5 1999/03/07 08:36:40 dirk Exp $
4 * This file is part of WorkMan, the civilized CD player library
5 * (c) 1991-1997 by Steven Grimm (original author)
6 * (c) by Dirk Försterling (current 'author' = maintainer)
7 * The maintainer can be contacted by his e-mail address:
8 * milliByte@DeathsDoor.com
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library 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 GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the Free
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * BSD/386-specific drive control routines.
30 static char plat_bsd386_id
[] = "$Id: plat_bsd386.c,v 1.5 1999/03/07 08:36:40 dirk Exp $";
34 #include <sys/types.h>
37 #include <sys/param.h>
40 #include "include/wm_config.h"
41 #define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
44 * The following is included from the Linux module. However, I didn't
45 * see a check here if the CD to be ejected is mounted...
47 #if defined(BSD_MOUNTTEST)
51 * this is for glibc 2.x which defines ust structure in
55 #include <sys/ustat.h>
62 #include <sys/cdrom.h>
64 # include <i386/isa/sblast.h>
67 #include "include/wm_struct.h"
70 * Since we can't sense the drive type with libcdrom anyway, and since the
71 * library doesn't provide "pause" or "resume" functions, use the daux field
72 * to point to the frame number at which we paused.
78 #define PAUSE_FRAME (((struct pause_info *) d->daux)->frame)
79 #define END_FRAME (((struct pause_info *) d->daux)->endframe)
80 #define CUR_CD ((struct cdinfo *) d->aux)
84 extern char *cd_device
;
89 int min_volume_drive
= 10; /* Toshiba drive does low values. */
90 int max_volume_drive
= 255;
91 #else /* not SOUNDBLASTER, libcdrom only */
96 /*--------------------------------------------------------*
97 * Initialize the drive. A no-op for the generic driver.
98 *--------------------------------------------------------*/
100 gen_init(struct wm_drive
*d
)
105 /*-------------------------------------*
106 * Get the number of tracks on the CD.
107 *-------------------------------------*/
109 gen_get_trackcount(struct wm_drive
*d
, int *tracks
)
111 *tracks
= CUR_CD
->ntracks
;
116 /*---------------------------------------------------------*
117 * Get the start time and mode (data or audio) of a track.
118 *---------------------------------------------------------*/
120 gen_get_trackinfo(struct wm_drive
*d
, int track
, int *data
, int *startframe
)
122 *data
= (CUR_CD
->tracks
[track
- 1].control
& 4) ? 1 : 0;
123 *startframe
= CUR_CD
->tracks
[track
- 1].start_frame
;
128 /*-------------------------------------*
129 * Get the number of frames on the CD.
130 *-------------------------------------*/
132 gen_get_cdlen(struct wm_drive
*d
, int *frames
)
134 *frames
= CUR_CD
->total_frames
;
139 /*--------------------------------------------------------------------------*
140 * Get the current status of the drive: the current play mode, the absolute
141 * position from start of disc (in frames), and the current track and index
142 * numbers if the CD is playing or paused.
143 *--------------------------------------------------------------------------*/
145 gen_get_drive_status(struct wm_drive
*d
, enum wm_cd_modes oldmode
,
146 enum wm_cd_modes
*mode
, int *pos
, int *track
, int *index
)
148 struct cdstatus status
;
149 extern enum wm_cd_modes cur_cdmode
;
151 /* If we can't get status, the CD is ejected, so default to that. */
152 *mode
= WM_CDM_EJECTED
;
154 /* Is the device open? */
157 switch (wmcd_open(d
)) {
166 if (cdstatus (CUR_CD
, &status
) < 0)
168 *mode
= WM_CDM_TRACK_DONE
; /* waiting for next track. */
173 *pos = status.abs_frame; \
174 *track = status.track_num; \
175 *index = status.index_num
177 switch (status
.state
) {
178 case cdstate_playing
:
179 *mode
= WM_CDM_PLAYING
;
183 case cdstate_stopped
:
184 /* the MITSUMI drive doesn't have a "paused" state,
185 so it always comes here and not to the paused section.
186 The PAUSE_FRAME stuff below (in gen_pause())
187 fakes out the paused state. */
188 if (oldmode
== WM_CDM_PLAYING
) {
189 *mode
= WM_CDM_TRACK_DONE
;
191 } else if (cur_cdmode
!= WM_CDM_PAUSED
) {
192 *mode
= WM_CDM_STOPPED
;
196 /* fall through if paused */
199 /* the SCSI2 code in the cdrom library only pauses with
200 cdstop(); it never truly stops a disc (until an in-progress
201 play reaches the end). So it always comes here. */
202 if (cur_cdmode
== WM_CDM_STOPPED
) {
203 *mode
= WM_CDM_STOPPED
;
207 if (oldmode
== WM_CDM_PLAYING
|| oldmode
== WM_CDM_PAUSED
) {
208 *mode
= WM_CDM_PAUSED
;
211 *mode
= WM_CDM_STOPPED
;
217 *mode
= WM_CDM_STOPPED
;
223 /*------------------------------------------------------------------------*
224 * Return a volume value suitable for passing to the CD-ROM drive. "vol"
225 * is a volume slider setting; "max" is the slider's maximum value.
226 *------------------------------------------------------------------------*/
228 scale_volume(int vol
, int max
)
230 /* on Toshiba XM-3401B drive, and on soundblaster, this works fine. */
231 return ((vol
* (max_volume
- min_volume
)) / max
+ min_volume
);
234 /*---------------------------------------------------------------------*
235 * Set the volume level for the left and right channels. Their values
236 * range from 0 to 100.
237 *---------------------------------------------------------------------*/
239 gen_set_volume(struct wm_drive
*d
, int left
, int right
)
241 left
= scale_volume(left
, 100);
242 right
= scale_volume(right
, 100);
245 /* Send a Mixer IOCTL */
247 struct sb_mixer_levels levels
;
248 if (ioctl(d
->fd
, MIXER_IOCTL_READ_LEVELS
, &levels
) == 0) {
249 levels
.cd
.l
= left
< 0 ? 0 : left
;
250 levels
.cd
.r
= right
< 0 ? 0 : right
;
251 (void) ioctl(d
->fd
, MIXER_IOCTL_SET_LEVELS
, &levels
);
253 perror("SoundBlaster mixer read failed");
256 /* NOTE: the cdvolume2() call is an addition to the cdrom library.
257 Pick it up from the archives on bsdi.com */
258 cdvolume2 (CUR_CD
, left
< 0 ? 0 : left
> 255 ? 255 : left
,
259 right
< 0 ? 0 : right
> 255 ? 255 : right
);
264 /*--------------------------------------------------------------------*
265 * Pause the CD. This is a bit of a trick since there's no cdpause()
266 * function in the library. We fake it by saving the frame number
268 *--------------------------------------------------------------------*/
270 gen_pause(struct wm_drive
*d
)
272 struct cdstatus status
;
274 if (cdstatus(d
->aux
, &status
) < 0)
276 if (status
.state
!= cdstate_playing
)
277 PAUSE_FRAME
= CUR_CD
->tracks
[0].start_frame
;
279 PAUSE_FRAME
= status
.abs_frame
;
280 if (cdstop(d
->aux
) < 0)
286 /*-------------------------------------------------*
287 * Resume playing the CD (assuming it was paused.)
288 *-------------------------------------------------*/
290 gen_resume(struct wm_drive
*d
)
294 status
= (d
->play
)(d
, PAUSE_FRAME
, END_FRAME
);
303 gen_stop(struct wm_drive
*d
)
305 return cdstop(d
->aux
);
308 /*------------------------------------------------------------*
309 * Play the CD from one position to another (both in frames.)
310 *------------------------------------------------------------*/
312 gen_play(struct wm_drive
*d
, int start
, int end
)
315 if (cdplay(d
->aux
, start
, end
) < 0)
321 /*----------------------------------------*
322 * Eject the current CD, if there is one.
323 *----------------------------------------*/
325 gen_eject(struct wm_drive
*d
)
336 /*----------------------------------------*
338 *----------------------------------------*/
339 int gen_closetray(struct wm_drive
*d
)
345 return(wmcd_open(d
));
350 /* Always succeed if the drive can't close */
352 #endif /* CAN_CLOSE */
353 } /* gen_closetray() */
355 /*---------------------------------------------------------------------------*
356 * unscale_volume(cd_vol, max)
358 * Given a value between min_volume and max_volume, return the volume slider
359 * value needed to achieve that value.
361 * Rather than perform floating-point calculations to reverse the above
362 * formula, we simply do a binary search of scale_volume()'s return values.
363 *--------------------------------------------------------------------------*/
365 unscale_volume(int cd_vol
, int max
)
367 int vol
= 0, top
= max
, bot
= 0, scaled
;
371 vol
= (top
+ bot
) / 2;
372 scaled
= scale_volume(vol
, max
);
373 if (cd_vol
== scaled
)
389 /*---------------------------------------------------------------------*
390 * Read the initial volume from the drive, if available. Each channel
391 * ranges from 0 to 100, with -1 indicating data not available.
392 *---------------------------------------------------------------------*/
394 gen_get_volume(struct wm_drive
*d
, int *left
, int *right
)
396 /* Most systems can't seem to do this... */
400 /* Send a Mixer IOCTL */
402 struct sb_mixer_levels levels
;
403 if (ioctl(d
->fd
, MIXER_IOCTL_READ_LEVELS
, &levels
) == 0) {
404 *left
= unscale_volume(levels
.cd
.l
, 100);
405 *right
= unscale_volume(levels
.cd
.r
, 100);
407 perror("SoundBlaster mixer read failed");
414 /*---------------------------------------------*
415 * Send an arbitrary SCSI command to a device.
416 *---------------------------------------------*/
418 wm_scsi(struct wm_drive
*d
, unsigned char *cdb
, int cdblen
,
419 void *retbuf
, int retbuflen
, int getreply
)
421 /* Don't know how to do SCSI passthrough... */
426 int sb_fd
= -3; /* patchable from external programs */
429 /*-----------------------------------------------------------------------*
430 * Open the CD device. We can't determine the drive type under BSD/386.
431 *-----------------------------------------------------------------------*/
433 wmcd_open(struct wm_drvie
*d
)
435 void *aux
= NULL
, *daux
= NULL
;
438 if (d
->aux
) /* Device already open? */
441 if ((aux
= cdopen(cd_device
)) == NULL
)
443 fprintf(stderr
, "No cdrom found by libcdrom\n");
447 if ((daux
= malloc(sizeof(struct pause_info
))) == NULL
)
451 if (sb_fd
!= -3 && sb_fd
< 0)
452 fd
= open ("/dev/sb_mixer", O_RDWR
, 0);
455 fprintf (stderr
,"SoundBlaster mixer not found/disabled; will use direct CD volume control\n");
456 max_volume
= max_volume_drive
;
457 min_volume
= min_volume_drive
;
461 /* Now fill in the relevant parts of the wm_drive structure. */
462 *d
= *(find_drive_struct("", "", ""));
475 * Re-Open the device if it is open.
478 wmcd_reopen( struct wm_drive
*d
)
483 wm_lib_message(WM_MSG_LEVEL_DEBUG
|WM_MSG_CLASS
, "wmcd_reopen ");
484 if (d
->fd
>= 0) /* Device really open? */
486 wm_lib_message(WM_MSG_LEVEL_DEBUG
|WM_MSG_CLASS
, "closes the device and ");
487 status
= close( d
->fd
); /* close it! */
488 /* we know, that the file is closed, do we? */
492 wm_lib_message(WM_MSG_LEVEL_DEBUG
|WM_MSG_CLASS
, "calls wmcd_open()\n");
493 status
= wmcd_open( d
); /* open it as usual */
495 } while ( status
!= 0 );
497 } /* wmcd_reopen() */
500 #endif /* __bsdi__ */