2
* $Id: plat_openbsd.c,v 1.7 2001/04/10 17:34:52 dfoerste 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
* OpenBSD-specific drive control routines. (Based on plat_freebsd.c)
27
* Michael Shalayeff, 7/24/96
32
static char plat_openbsd[] = "$Id: plat_openbsd.c,v 1.7 2001/04/10 17:34:52 dfoerste Exp $";
34
#if defined(__OpenBSD__) || defined(__OpenBSD)
39
#include <sys/types.h>
40
#include <sys/param.h>
41
#include <sys/mount.h>
43
#include <sys/param.h>
46
#include "include/wm_config.h"
48
/* this is for glibc 2.x which defines the ust structure in ustat.h not stat.h */
50
#include <sys/ustat.h>
55
#include <sys/ioctl.h>
57
#include <sys/scsiio.h>
58
#include <scsi/scsi_all.h>
59
#include <scsi/scsi_cd.h>
61
#include "include/wm_struct.h"
62
#include "include/wm_cdtext.h"
64
#define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
71
extern char *cd_device;
75
* Initialize the drive. A no-op for the generic driver.
78
gen_init(struct wm_drive *d)
85
* Open the CD device and figure out what kind of drive is attached.
88
wmcd_open(struct wm_drive *d)
91
static int warned = 0;
92
char vendor[32] = WM_STR_GENVENDOR;
93
char model[32] = WM_STR_GENMODEL;
94
char rev[32] = WM_STR_GENREV;
96
if (d->fd >= 0) /* Device already open? */
98
wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_open(): [device is open (fd=%d)]\n", d->fd);
102
if (cd_device == NULL)
103
cd_device = DEFAULT_CD_DEVICE;
105
d->fd = open(cd_device, 0);
113
/* No CD in drive. */
117
/* Now fill in the relevant parts of the wm_drive structure. */
120
*d = *(find_drive_struct(vendor, model, rev));
121
wm_drive_settype(vendor, model, rev);
131
* Re-Open the device if it is open.
134
wmcd_reopen( struct wm_drive *d )
139
wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen\n");
140
if (d->fd >= 0) /* Device really open? */
142
wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closing the device\n");
143
status = close( d->fd ); /* close it! */
144
/* we know, that the file is closed, do we? */
148
wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "calling wmcd_open()\n");
149
status = wmcd_open( d ); /* open it as usual */
151
} while ( status != 0 );
153
} /* wmcd_reopen() */
156
* Send an arbitrary SCSI command to a device.
160
wm_scsi(struct wm_drive *d, unsigned char *cdb,
161
int cdblen, void *retbuf, int retbuflen, int getreply)
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.
172
gen_get_drive_status(struct wm_drive *d, enum wm_cd_modes oldmode,
173
enum wm_cd_modes *mode, int *pos, int *track, int *index)
175
struct ioc_read_subchannel sc;
176
struct cd_sub_channel_info scd;
178
/* If we can't get status, the CD is ejected, so default to that. */
179
*mode = WM_CDM_EJECTED;
181
sc.address_format = CD_MSF_FORMAT;
182
sc.data_format = CD_CURRENT_POSITION;
184
sc.data_len = sizeof(scd);
185
sc.data = (struct cd_sub_channel_info *)&scd;
187
/* Is the device open? */
190
switch (wmcd_open(d)) {
199
if (ioctl(d->fd, CDIOCREADSUBCHANNEL, &sc))
201
/* we need to release the device so the kernel will notice
205
return (0); /* ejected */
208
switch (scd.header.audio_status)
210
case CD_AS_PLAY_IN_PROGRESS:
211
*mode = WM_CDM_PLAYING;
213
*pos = scd.what.position.absaddr.msf.minute * 60 * 75 +
214
scd.what.position.absaddr.msf.second * 75 +
215
scd.what.position.absaddr.msf.frame;
216
*track = scd.what.position.track_number;
217
*index = scd.what.position.index_number;
220
case CD_AS_PLAY_PAUSED:
221
if (oldmode == WM_CDM_PLAYING || oldmode == WM_CDM_PAUSED)
223
*mode = WM_CDM_PAUSED;
227
*mode = WM_CDM_STOPPED;
230
case CD_AS_PLAY_COMPLETED:
231
*mode = WM_CDM_TRACK_DONE; /* waiting for next track. */
234
case CD_AS_NO_STATUS:
236
*mode = WM_CDM_STOPPED;
241
} /* gen_get_drive_status() */
244
* Get the number of tracks on the CD.
247
gen_get_trackcount(struct wm_drive *d, int tracks)
249
struct ioc_toc_header hdr;
251
if (ioctl(d->fd, CDIOREADTOCHEADER, &hdr) == -1)
254
*tracks = hdr.ending_track - hdr.starting_track + 1;
256
} /* gen_get_trackcount() */
259
* Get the start time and mode (data or audio) of a track.
261
* XXX - this should get cached, but that means keeping track of ejects.
264
gen_get_trackinfo(struct wm_drive *d, int track, int *data, int *startframe)
266
struct ioc_read_toc_entry toc;
267
struct cd_toc_entry toc_buffer;
269
bzero((char *)&toc_buffer, sizeof(toc_buffer));
270
toc.address_format = CD_MSF_FORMAT;
271
toc.starting_track = track;
272
toc.data_len = sizeof(toc_buffer);
273
toc.data = &toc_buffer;
275
if (ioctl(d->fd, CDIOREADTOCENTRYS, &toc))
278
*data = ((toc_buffer.control & 0x4) != 0);
280
*startframe = toc_buffer.addr.msf.minute*60*75 +
281
toc_buffer.addr.msf.second * 75 +
282
toc_buffer.addr.msf.frame;
285
} /* gen_get_trackinfo() */
288
* Get the number of frames on the CD.
291
gen_get_cdlen(struct wm_drive *d, int *frames)
294
struct ioc_toc_header hdr;
297
#define LEADOUT 0xaa /* see scsi.c. what a hack! */
298
return gen_get_trackinfo(d, LEADOUT, &tmp, frames);
299
} /* gen_get_cdlen() */
302
* Play the CD from one position to another (both in frames.)
305
gen_play(struct wm_drive *d, int start, int end)
307
struct ioc_play_msf msf;
309
msf.start_m = start / (60*75);
310
msf.start_s = (start % (60*75)) / 75;
311
msf.start_f = start % 75;
312
msf.end_m = end / (60*75);
313
msf.end_s = (end % (60*75)) / 75;
314
msf.end_f = end % 75;
316
if (ioctl(d->fd, CDIOCSTART))
319
if (ioctl(d->fd, CDIOCPLAYMSF, &msf))
329
gen_pause(struct wm_drive *d)
331
return (ioctl(d->fd, CDIOCPAUSE));
335
* Resume playing the CD (assuming it was paused.)
338
gen_resume(struct wm_drive *d)
340
return (ioctl(d->fd, CDIOCRESUME));
347
gen_stop(struct wm_drive *d)
349
return (ioctl(d->fd, CDIOCSTOP));
353
* Eject the current CD, if there is one.
356
gen_eject(struct wm_drive *d)
358
/* On some systems, we can check to see if the CD is mounted. */
363
if (fstat(d->fd, &stbuf) != 0)
366
/* Is this a mounted filesystem? */
367
if (fstatfs(stbuf.st_rdev, &buf) == 0)
370
rval = ioctl(d->fd, CDIOCALLOW);
372
rval = ioctl(d->fd, CDIOCEJECT);
374
rval = ioctl(d->fd, CDIOCPREVENT);
382
/*----------------------------------------*
385
* Please edit and send changes to
386
* milliByte@DeathsDoor.com
387
*----------------------------------------*/
390
gen_closetray(struct wm_drive *d)
396
return(wmcd_reopen(d));
401
/* Always succeed if the drive can't close */
403
#endif /* CAN_CLOSE */
404
} /* gen_closetray() */
407
* scale_volume(vol, max)
409
* Return a volume value suitable for passing to the CD-ROM drive. "vol"
410
* is a volume slider setting; "max" is the slider's maximum value.
412
* On Sun and DEC CD-ROM drives, the amount of sound coming out the jack
413
* increases much faster toward the top end of the volume scale than it
414
* does at the bottom. To make up for this, we make the volume scale look
415
* sort of logarithmic (actually an upside-down inverse square curve) so
416
* that the volume value passed to the drive changes less and less as you
417
* approach the maximum slider setting. The actual formula looks like
419
* (max^2 - (max - vol)^2) * (max_volume - min_volume)
420
* v = --------------------------------------------------- + min_volume
423
* If your system's volume settings aren't broken in this way, something
424
* like the following should work:
426
* return ((vol * (max_volume - min_volume)) / max + min_volume);
429
scale_volume(int vol, int max)
431
return ((vol * (max_volume - min_volume)) / max + min_volume);
432
} /* scale_volume() */
435
* unscale_volume(cd_vol, max)
437
* Given a value between min_volume and max_volume, return the volume slider
438
* value needed to achieve that value.
440
* Rather than perform floating-point calculations to reverse the above
441
* formula, we simply do a binary search of scale_volume()'s return values.
444
unscale_volume(int cd_vol, int max)
446
int vol = 0, top = max, bot = 0, scaled;
450
vol = (top + bot) / 2;
451
scaled = scale_volume(vol, max);
452
if (cd_vol == scaled)
466
} /* unscale_volume() */
469
* Set the volume level for the left and right channels. Their values
470
* range from 0 to 100.
473
gen_set_volume(struct wm_drive *d, int left, int right)
477
if (left < 0) /* don't laugh, I saw this happen once! */
481
left = scale_volume(left, 100);
482
right = scale_volume(right, 100);
484
bzero((char *)&vol, sizeof(vol));
486
vol.vol[LEFT_PORT] = left;
487
vol.vol[RIGHT_PORT] = right;
489
if (ioctl(d->fd, CDIOCSETVOL, &vol))
493
} /* gen_set_volume() */
497
* Read the initial volume from the drive, if available. Each channel
498
* ranges from 0 to 100, with -1 indicating data not available.
501
gen_get_volume(struct wm_drive *d, int *left, int *right)
507
bzero((char *)&vol, sizeof(vol));
509
if (ioctl(d->fd, CDIOCGETVOL, &vol))
513
*left = unscale_volume(vol.vol[LEFT_PORT], 100);
514
*right = unscale_volume(vol.vol[RIGHT_PORT], 100);
521
} /* gen_get_volume() */
523
/*------------------------------------------------------------------------*
524
* gen_get_cdtext(drive, buffer, lenght)
525
*------------------------------------------------------------------------*/
528
gen_get_cdtext(struct wm_drive *d, unsigned char **pp_buffer, int *p_buffer_lenght)
530
return -1; /* no SCSI, no CDTEXT */
531
} /* gen_get_cdtext() */