2
* $Id: plat_ultrix.c,v 1.9 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
* ULTRIX 4.2 drive control routines.
28
static char plat_ultrix_id[] = "$Id: plat_ultrix.c,v 1.9 2001/04/10 17:34:52 dfoerste Exp $";
30
#if defined(ultrix) || defined(__ultrix)
35
#include <sys/types.h>
37
#include <sys/param.h>
42
#include <sys/rzdisk.h>
43
#include <sys/cdrom.h>
45
#include "include/wm_config.h"
46
#include "include/wm_cdtext.h"
47
#include "include/wm_struct.h"
49
#define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
52
* This structure will be filled with the TOC header and all entries.
53
* Ultrix doesn't seem to allow getting single TOC entries.
54
* - Chris Ross (cross@eng.umd.edu)
56
struct cd_toc_header_and_entries
58
struct cd_toc_header cdth;
59
struct cd_toc_entry cdte[CDROM_MAX_TRACK+1];
68
extern char *cd_device;
73
* Simulate fgets, but joining continued lines in the output of uerf.
76
#define BUF_SIZE 85 /* Max length of a (real) line */
81
static char *retval = NULL;
82
static char holdbuf[BUF_SIZE + 1];
83
char tmp[BUF_SIZE + 1];
88
retval = malloc(BUF_SIZE * 3); /* 3 lines can be joined */
97
strcpy(retval, holdbuf);
98
retval[strlen(retval)-1] = '\0';
99
memset(holdbuf, 0, BUF_SIZE+1);
102
while (fgets(tmp, BUF_SIZE, fp))
104
stmp = tmp + strspn(tmp, " \t");
106
{ /* Continuation line */
107
retval[strlen(retval)-1] = '\0'; /* Trim off C/R */
108
strcat(retval, stmp+1);
112
strcpy(holdbuf, tmp);
113
holdbuf[strlen(holdbuf)-1] = -1;
115
} else { /* First line read, keep reading */
116
strcat(retval, stmp);
117
retval[strlen(retval)-1] = '\0';
127
* Determine the name of the CD-ROM device.
129
* Read through the boot records (via a call to uerf) and find the SCSI
130
* address of the CD-ROM. If the "CDROM" environment variable is set,
141
cd_device = getenv("CDROM");
143
if (cd_device != NULL)
145
if(strncmp("/dev/", cd_device, 5) || strstr(cd_device, "/../"))
151
if ((pid = fork()) == 0)
155
execl("/etc/uerf", "uerf", "-R", "-r", "300", NULL);
156
execl("/usr/sbin/uerf", "uerf", "-R", "-r", "300", NULL);
157
return 0; /* _exit(1); */
158
} else if (pid < 0) {
160
return 0; /* exit(1); */
164
uerf = fdopen(fds[0], "r");
166
while (data = fgetline(uerf))
167
if (strstr(data, "RRD42"))
171
cd_device = (char *)malloc(sizeof("/dev/rrz##c"));
172
strcpy(cd_device, "/dev/r");
173
device = strstr(data, "rz");
174
device[(int)(strchr(device, ' ') - device)] = '\0';
175
strcat(cd_device, device);
176
strcat(cd_device, "c");
182
if (cd_device == NULL)
185
"No cdrom (RRD42) is installed on this system\n");
186
return 0; /* exit(1); */
190
(void)wait((int *)NULL);
195
* initialize the drive. a no-op for the generic driver.
198
gen_init( struct wm_drive *d )
205
* Open the CD device and figure out what kind of drive is attached.
208
wmcd_open( struct wm_drive *d )
211
static int warned = 0;
213
if (d->fd >= 0) /* Device already open? */
215
wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_open(): [device is open (fd=%d)]\n", d->fd);
219
if (cd_device == NULL)
222
d->fd = open(cd_device, 0);
229
else if (errno != EINTR)
234
/* No CD in drive. */
238
/* Now fill in the relevant parts of the wm_drive structure. */
240
*d = *(find_drive_struct("", "", ""));
249
* Re-Open the device if it is open.
252
wmcd_reopen( struct wm_drive *d )
257
wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen\n");
258
if (d->fd >= 0) /* Device really open? */
260
wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closing the device\n");
261
status = close( d->fd ); /* close it! */
262
/* we know, that the file is closed, do we? */
266
wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "calling wmcd_open()\n");
267
status = wmcd_open( d ); /* open it as usual */
269
} while ( status != 0 );
271
} /* wmcd_reopen() */
274
* Send an arbitrary SCSI command to a device.
277
wm_scsi( struct wm_drive *d, unsigned char *cdb, int cdblen,
278
void *retbuf, int retbuflen, int getreply )
280
/* ULTRIX doesn't have a SCSI passthrough interface, does it? */
286
* Get the current status of the drive: the current play mode, the absolute
287
* position from start of disc (in frames), and the current track and index
288
* numbers if the CD is playing or paused.
291
gen_get_drive_status( struct wm_drive *d, enum wm_cd_modes oldmode,
292
enum wm_cd_modes *mode, int *pos, int *track, int *index)
294
struct cd_sub_channel sc;
295
struct cd_subc_channel_data scd;
297
/* If we can't get status, the CD is ejected, so default to that. */
298
*mode = WM_CDM_EJECTED;
300
sc.sch_address_format = CDROM_MSF_FORMAT;
301
sc.sch_data_format = CDROM_CURRENT_POSITION;
302
sc.sch_track_number = 0;
303
sc.sch_alloc_length = sizeof(scd);
304
sc.sch_buffer = (caddr_t)&scd;
306
/* Is the device open? */
309
switch (wmcd_open(d))
319
if (ioctl(d->fd, CDROM_READ_SUBCHANNEL, &sc))
320
return (0); /* ejected */
322
switch (scd.scd_header.sh_audio_status)
324
case AS_PLAY_IN_PROGRESS:
325
*mode = WM_CDM_PLAYING;
327
*pos = scd.scd_position_data.scp_absaddr.msf.m_units * 60 * 75 +
328
scd.scd_position_data.scp_absaddr.msf.s_units * 75 +
329
scd.scd_position_data.scp_absaddr.msf.f_units;
330
*track = scd.scd_position_data.scp_track_number;
331
*index = scd.scd_position_data.scp_index_number;
335
if (oldmode == WM_CDM_PLAYING || oldmode == WM_CDM_PAUSED)
337
*mode = WM_CDM_PAUSED;
341
*mode = WM_CDM_STOPPED;
344
case AS_PLAY_COMPLETED:
345
*mode = WM_CDM_TRACK_DONE; /* waiting for next track. */
349
*mode = WM_CDM_STOPPED;
353
} /* gen_get_drive_status() */
356
* Get the number of tracks on the CD.
359
gen_get_trackcount( struct wm_drive *d, int *tracks )
361
struct cd_toc_header hdr;
363
if (ioctl(d->fd, CDROM_TOC_HEADER, &hdr))
366
*tracks = hdr.th_ending_track;
369
} /* gen_get_trackcount() */
372
* Get the start time and mode (data or audio) of a track.
374
* XXX - this should get cached, but that means keeping track of ejects.
377
gen_get_trackinfo(struct wm_drive *d, int track, int *data, int *startframe)
380
struct cd_toc_header hdr;
381
struct cd_toc_header_and_entries toc_buffer;
383
if (ioctl(d->fd, CDROM_TOC_HEADER, &hdr))
386
bzero((char *)&toc_buffer, sizeof(toc_buffer));
387
toc.toc_address_format = CDROM_MSF_FORMAT;
388
toc.toc_starting_track = 0;
389
toc.toc_alloc_length = (u_short)(((hdr.th_data_len1 << 8) +
390
hdr.th_data_len0) & 0xfff) + 2;
391
toc.toc_buffer = (caddr_t)&toc_buffer;
393
if (ioctl(d->fd, CDROM_TOC_ENTRYS, &toc))
397
track = hdr.th_ending_track + 1;
399
*data = (toc_buffer.cdte[track-1].te_control & CDROM_DATA_TRACK) ? 1:0;
400
*startframe = toc_buffer.cdte[track - 1].te_absaddr.msf.m_units*60*75 +
401
toc_buffer.cdte[track - 1].te_absaddr.msf.s_units * 75 +
402
toc_buffer.cdte[track - 1].te_absaddr.msf.f_units;
405
} /* gen_get_trackinfo() */
408
* Get the number of frames on the CD.
411
gen_get_cdlen(struct wm_drive *d, int *frames)
414
return (gen_get_trackinfo(d, 0, &tmp, frames));
415
} /* gen_get_cdlen() */
418
* Play the CD from one position to another (both in frames.)
421
gen_play( struct wm_drive *d, int start, int end )
423
struct cd_play_audio_msf msf;
425
msf.msf_starting_M_unit = start / (60*75);
426
msf.msf_starting_S_unit = (start % (60*75)) / 75;
427
msf.msf_starting_F_unit = start % 75;
428
msf.msf_ending_M_unit = end / (60*75);
429
msf.msf_ending_S_unit = (end % (60*75)) / 75;
430
msf.msf_ending_F_unit = end % 75;
432
if (ioctl(d->fd, SCSI_START_UNIT))
434
if (ioctl(d->fd, CDROM_PLAY_MSF, &msf))
444
gen_pause( struct wm_drive *d )
446
return (ioctl(d->fd, CDROM_PAUSE_PLAY));
450
* Resume playing the CD (assuming it was paused.)
453
gen_resume( struct wm_drive *d )
455
return (ioctl(d->fd, CDROM_RESUME_PLAY));
462
gen_stop( struct wm_drive *d )
464
return (ioctl(d->fd, SCSI_STOP_UNIT));
468
* Eject the current CD, if there is one.
471
gen_eject(struct wm_drive *d)
473
/* On some systems, we can check to see if the CD is mounted. */
477
if (fstat(d->fd, &stbuf) != 0)
480
/* Is this a mounted filesystem? */
481
if (ustat(stbuf.st_rdev, &ust) == 0)
484
return (ioctl(d->fd, CDROM_EJECT_CADDY));
487
/*----------------------------------------*
490
* Please edit and send changes to
491
* milliByte@DeathsDoor.com
492
*----------------------------------------*/
495
gen_closetray(struct wm_drive *d)
501
return(wmcd_reopen(d));
506
/* Always succeed if the drive can't close */
508
#endif /* CAN_CLOSE */
509
} /* gen_closetray() */
513
* scale_volume(vol, max)
515
* Return a volume value suitable for passing to the CD-ROM drive. "vol"
516
* is a volume slider setting; "max" is the slider's maximum value.
518
* On Sun and DEC CD-ROM drives, the amount of sound coming out the jack
519
* increases much faster toward the top end of the volume scale than it
520
* does at the bottom. To make up for this, we make the volume scale look
521
* sort of logarithmic (actually an upside-down inverse square curve) so
522
* that the volume value passed to the drive changes less and less as you
523
* approach the maximum slider setting. The actual formula looks like
525
* (max^2 - (max - vol)^2) * (max_volume - min_volume)
526
* v = --------------------------------------------------- + min_volume
529
* If your system's volume settings aren't broken in this way, something
530
* like the following should work:
532
* return ((vol * (max_volume - min_volume)) / max + min_volume);
534
scale_volume( int vol, int max )
536
return ((max * max - (max - vol) * (max - vol)) *
537
(max_volume - min_volume) / (max * max) + min_volume);
538
} /* scale_volume() */
542
* unscale_volume(cd_vol, max)
544
* Given a value between min_volume and max_volume, return the volume slider
545
* value needed to achieve that value.
547
* Rather than perform floating-point calculations to reverse the above
548
* formula, we simply do a binary search of scale_volume()'s return values.
551
unscale_volume( int cd_vol, int max )
553
int vol = 0, top = max, bot = 0, scaled;
557
vol = (top + bot) / 2;
558
scaled = scale_volume(vol, max);
559
if (cd_vol == scaled)
573
} /* unscale_volume() */
576
* Set the volume level for the left and right channels. Their values
577
* range from 0 to 100.
580
gen_set_volume( struct wm_drive *d, int left, int right )
582
struct cd_playback pb;
583
struct cd_playback_status ps;
584
struct cd_playback_control pc;
586
left = scale_volume(left, 100);
587
right = scale_volume(right, 100);
589
bzero((char *)&pb, sizeof(pb));
590
bzero((char *)&ps, sizeof(ps));
591
bzero((char *)&pc, sizeof(pc));
593
pb.pb_alloc_length = sizeof(ps);
594
pb.pb_buffer = (caddr_t)&ps;
596
if (ioctl(d->fd, CDROM_PLAYBACK_STATUS, &pb))
599
pc.pc_chan0_select = ps.ps_chan0_select;
600
pc.pc_chan0_volume = (left < CDROM_MIN_VOLUME) ?
601
CDROM_MIN_VOLUME : (left > CDROM_MAX_VOLUME) ?
602
CDROM_MAX_VOLUME : left;
603
pc.pc_chan1_select = ps.ps_chan1_select;
604
pc.pc_chan1_volume = (right < CDROM_MIN_VOLUME) ?
605
CDROM_MIN_VOLUME : (right > CDROM_MAX_VOLUME) ?
606
CDROM_MAX_VOLUME : right;
608
pb.pb_alloc_length = sizeof(pc);
609
pb.pb_buffer = (caddr_t)&pc;
611
if (ioctl(d->fd, CDROM_PLAYBACK_CONTROL, &pb))
615
} /* gen_set_volume() */
618
* Read the initial volume from the drive, if available. Each channel
619
* ranges from 0 to 100, with -1 indicating data not available.
622
gen_get_volume(struct wm_drive *d, int *left, int *right)
624
struct cd_playback pb;
625
struct cd_playback_status ps;
627
bzero((char *)&pb, sizeof(pb));
628
bzero((char *)&ps, sizeof(ps));
630
pb.pb_alloc_length = sizeof(ps);
631
pb.pb_buffer = (caddr_t)&ps;
635
if (ioctl(d->fd, CDROM_PLAYBACK_STATUS, &pb))
639
*left = unscale_volume(ps.ps_chan0_volume, 100);
640
*right = unscale_volume(ps.ps_chan1_volume, 100);
647
} /* gen_get_volume() */
649
/*------------------------------------------------------------------------*
650
* gen_get_cdtext(drive, buffer, lenght)
651
*------------------------------------------------------------------------*/
654
gen_get_cdtext(struct wm_drive *d, unsigned char **pp_buffer, int *p_buffer_lenght)
656
return -1; /* No SCSI, no CDTEXT */
657
} /* gen_get_cdtext() */