wmmisc: Bump to version 1.2.
[dockapps.git] / ascd / libworkman / plat_freebsd.c
blob010b2bc77764520f45cf8edb9d171a5a0d626a97
1 /*
2 * $Id: plat_freebsd.c,v 1.8 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 * plat_freebsd.c
27 * FreeBSD-specific drive control routines.
29 * Todd Pfaff, 3/20/94
33 #if defined(__FreeBSD__) || defined(__NetBSD__)
35 static char freebsd_id[] = "$Id: plat_freebsd.c,v 1.8 1999/03/07 08:36:40 dirk Exp $";
37 #include <errno.h>
38 #include <stdio.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <sys/stat.h>
47 #include "include/wm_config.h"
49 #include <sys/time.h>
50 #include <sys/ioctl.h>
51 #include <sys/cdio.h>
52 #include <sys/scsiio.h>
54 #if defined(__NetBSD__)
55 # define MSF_MINUTES 1
56 # define MSF_SECONDS 2
57 # define MSF_FRAMES 3
58 # include "/sys/scsi/scsi_all.h"
59 # include "/sys/scsi/scsi_cd.h"
60 #else
61 # define LEFT_PORT 0
62 # define RIGHT_PORT 1
63 # if __FreeBSD_version < 300000
64 # include <scsi.h>
65 # endif
66 #endif
68 #include "include/wm_struct.h"
69 #include "include/wm_platform.h"
70 #include "include/wm_cdrom.h"
71 #include "include/wm_scsi.h"
72 #include "include/wm_helpers.h"
74 #define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
76 void *malloc();
78 int min_volume = 10;
79 int max_volume = 255;
81 extern char *cd_device;
84 /*--------------------------------------------------------*
85 * Initialize the drive. A no-op for the generic driver.
86 *--------------------------------------------------------*/
87 int
88 gen_init(struct wm_drive *d)
90 return (0);
93 /*-------------------------------------*
94 * Get the number of tracks on the CD.
95 *-------------------------------------*/
96 int
97 gen_get_trackcount(struct wm_drive *d, int *tracks)
99 struct ioc_toc_header hdr;
101 if (ioctl(d->fd, CDIOREADTOCHEADER, &hdr) == -1)
102 return (-1);
104 *tracks = hdr.ending_track - hdr.starting_track + 1;
106 return (0);
109 /*-----------------------------------------------------------------------*
110 * Get the start time and mode (data or audio) of a track.
112 * XXX - this should get cached, but that means keeping track of ejects.
113 *-----------------------------------------------------------------------*/
115 gen_get_trackinfo(struct wm_drive *d, int track, int *data, int *startframe)
117 struct ioc_read_toc_entry toc;
118 struct cd_toc_entry toc_buffer;
120 bzero((char *)&toc_buffer, sizeof(toc_buffer));
121 toc.address_format = CD_MSF_FORMAT;
122 toc.starting_track = track;
123 toc.data_len = sizeof(toc_buffer);
124 toc.data = &toc_buffer;
126 if (ioctl(d->fd, CDIOREADTOCENTRYS, &toc))
127 return (-1);
129 *data = ((toc_buffer.control & 0x4) != 0);
131 #ifdef __NetBSD__
132 *startframe = toc_buffer.addr[MSF_MINUTES]*60*75 +
133 toc_buffer.addr[MSF_SECONDS] * 75 +
134 toc_buffer.addr[MSF_FRAMES];
135 #else
136 *startframe = toc_buffer.addr.msf.minute*60*75 +
137 toc_buffer.addr.msf.second * 75 +
138 toc_buffer.addr.msf.frame;
139 #endif
141 return (0);
144 /*-------------------------------------*
145 * Get the number of frames on the CD.
146 *-------------------------------------*/
148 gen_get_cdlen(struct wm_drive *d, int *frames)
150 int tmp;
151 struct ioc_toc_header hdr;
152 int status;
154 #define LEADOUT 0xaa /* see scsi.c. what a hack! */
155 return gen_get_trackinfo(d, LEADOUT, &tmp, frames);
158 /*--------------------------------------------------------------------------*
159 * Get the current status of the drive: the current play mode, the absolute
160 * position from start of disc (in frames), and the current track and index
161 * numbers if the CD is playing or paused.
162 *--------------------------------------------------------------------------*/
164 gen_get_drive_status(struct wm_drive *d, enum wm_cd_modes oldmode,
165 enum wm_cd_modes *mode, int *pos, int *track, int *index)
167 struct ioc_read_subchannel sc;
168 struct cd_sub_channel_info scd;
170 /* If we can't get status, the CD is ejected, so default to that. */
171 *mode = WM_CDM_EJECTED;
173 sc.address_format = CD_MSF_FORMAT;
174 sc.data_format = CD_CURRENT_POSITION;
175 sc.track = 0;
176 sc.data_len = sizeof(scd);
177 sc.data = (struct cd_sub_channel_info *)&scd;
179 /* Is the device open? */
180 if (d->fd < 0)
182 switch (wmcd_open(d)) {
183 case -1: /* error */
184 return (-1);
186 case 1: /* retry */
187 return (0);
191 if (ioctl(d->fd, CDIOCREADSUBCHANNEL, &sc)) {
193 * #ifdef __NetBSD__
195 * Denis Bourez <denis@rsn.fdn.fr> told me, that closing the
196 * device is mandatory for FreeBSD, too.
198 /* we need to release the device so the kernel will notice
199 reloaded media */
200 (void) close(d->fd);
201 d->fd = -1;
203 * #endif
205 return (0); /* ejected */
208 switch (scd.header.audio_status) {
209 case CD_AS_PLAY_IN_PROGRESS:
210 *mode = WM_CDM_PLAYING;
211 dopos:
212 #ifdef __NetBSD__
213 *pos = scd.what.position.absaddr[MSF_MINUTES] * 60 * 75 +
214 scd.what.position.absaddr[MSF_SECONDS] * 75 +
215 scd.what.position.absaddr[MSF_FRAMES];
216 #else
217 *pos = scd.what.position.absaddr.msf.minute * 60 * 75 +
218 scd.what.position.absaddr.msf.second * 75 +
219 scd.what.position.absaddr.msf.frame;
220 #endif
221 *track = scd.what.position.track_number;
222 *index = scd.what.position.index_number;
223 break;
225 case CD_AS_PLAY_PAUSED:
226 if (oldmode == WM_CDM_PLAYING || oldmode == WM_CDM_PAUSED)
228 *mode = WM_CDM_PAUSED;
229 goto dopos;
231 else
232 *mode = WM_CDM_STOPPED;
233 break;
235 case CD_AS_PLAY_COMPLETED:
236 *mode = WM_CDM_TRACK_DONE; /* waiting for next track. */
237 break;
239 case CD_AS_NO_STATUS:
240 case 0:
241 *mode = WM_CDM_STOPPED;
242 break;
245 return (0);
248 /*---------------------------------------------------------------------------*
249 * scale_volume(vol, max)
251 * Return a volume value suitable for passing to the CD-ROM drive. "vol"
252 * is a volume slider setting; "max" is the slider's maximum value.
254 * On Sun and DEC CD-ROM drives, the amount of sound coming out the jack
255 * increases much faster toward the top end of the volume scale than it
256 * does at the bottom. To make up for this, we make the volume scale look
257 * sort of logarithmic (actually an upside-down inverse square curve) so
258 * that the volume value passed to the drive changes less and less as you
259 * approach the maximum slider setting. The actual formula looks like
261 * (max^2 - (max - vol)^2) * (max_volume - min_volume)
262 * v = --------------------------------------------------- + min_volume
263 * max^2
265 * If your system's volume settings aren't broken in this way, something
266 * like the following should work:
268 * return ((vol * (max_volume - min_volume)) / max + min_volume);
269 *---------------------------------------------------------------------------*/
270 static int
271 scale_volume(int vol, int max)
273 return ((vol * (max_volume - min_volume)) / max + min_volume);
276 /*---------------------------------------------------------------------*
277 * Set the volume level for the left and right channels. Their values
278 * range from 0 to 100.
279 *---------------------------------------------------------------------*/
281 gen_set_volume(struct wm_drive *d, int left, int right)
283 struct ioc_vol vol;
285 if (left < 0) /* don't laugh, I saw this happen once! */
286 left = 0;
287 if (right < 0)
288 right = 0;
289 left = scale_volume(left, 100);
290 right = scale_volume(right, 100);
292 bzero((char *)&vol, sizeof(vol));
294 vol.vol[LEFT_PORT] = left;
295 vol.vol[RIGHT_PORT] = right;
297 if (ioctl(d->fd, CDIOCSETVOL, &vol))
298 return (-1);
300 return (0);
303 /*---------------*
304 * Pause the CD.
305 *---------------*/
307 gen_pause( struct wm_drive *d )
309 return (ioctl(d->fd, CDIOCPAUSE));
312 /*-------------------------------------------------*
313 * Resume playing the CD (assuming it was paused.)
314 *-------------------------------------------------*/
316 gen_resume( struct wm_drive *d )
318 return (ioctl(d->fd, CDIOCRESUME));
321 /*--------------*
322 * Stop the CD.
323 *--------------*/
325 gen_stop( struct wm_drive *d)
327 return (ioctl(d->fd, CDIOCSTOP));
330 /*------------------------------------------------------------*
331 * Play the CD from one position to another (both in frames.)
332 *------------------------------------------------------------*/
334 gen_play(struct wm_drive *d, int start, int end )
336 struct ioc_play_msf msf;
338 msf.start_m = start / (60*75);
339 msf.start_s = (start % (60*75)) / 75;
340 msf.start_f = start % 75;
341 msf.end_m = end / (60*75);
342 msf.end_s = (end % (60*75)) / 75;
343 msf.end_f = end % 75;
345 if (ioctl(d->fd, CDIOCSTART))
346 return (-1);
348 if (ioctl(d->fd, CDIOCPLAYMSF, &msf))
349 return (-2);
351 return (0);
354 /*----------------------------------------*
355 * Eject the current CD, if there is one.
356 *----------------------------------------*/
358 gen_eject( struct wm_drive *d )
360 /* On some systems, we can check to see if the CD is mounted. */
361 struct stat stbuf;
362 struct statfs buf;
363 int rval;
365 if (fstat(d->fd, &stbuf) != 0)
366 return (-2);
368 /* Is this a mounted filesystem? */
369 if (fstatfs(stbuf.st_rdev, &buf) == 0)
370 return (-3);
372 rval = ioctl(d->fd, CDIOCALLOW);
374 if (rval == 0)
375 rval = ioctl(d->fd, CDIOCEJECT);
377 if (rval == 0)
378 rval = ioctl(d->fd, CDIOCPREVENT);
380 (void) close(d->fd);
382 return rval;
385 /*----------------------------------------*
386 * Close the CD tray
387 *----------------------------------------*/
389 int gen_closetray(struct wm_drive *d)
391 #ifdef CAN_CLOSE
392 if(!close(d->fd))
394 d->fd=-1;
395 return(wmcd_reopen(d));
396 } else {
397 return(-1);
399 #else
400 /* Always succeed if the drive can't close */
401 return(0);
402 #endif /* CAN_CLOSE */
403 } /* gen_closetray() */
406 /*---------------------------------------------------------------------------*
407 * unscale_volume(cd_vol, max)
409 * Given a value between min_volume and max_volume, return the volume slider
410 * value needed to achieve that value.
412 * Rather than perform floating-point calculations to reverse the above
413 * formula, we simply do a binary search of scale_volume()'s return values.
414 *--------------------------------------------------------------------------*/
415 static int
416 unscale_volume( int cd_vol, int max )
418 int vol = 0, top = max, bot = 0, scaled;
420 while (bot <= top)
422 vol = (top + bot) / 2;
423 scaled = scale_volume(vol, max);
424 if (cd_vol == scaled)
425 break;
426 if (cd_vol < scaled)
427 top = vol - 1;
428 else
429 bot = vol + 1;
432 if (vol < 0)
433 vol = 0;
434 else if (vol > max)
435 vol = max;
437 return (vol);
440 /*---------------------------------------------------------------------*
441 * Read the initial volume from the drive, if available. Each channel
442 * ranges from 0 to 100, with -1 indicating data not available.
443 *---------------------------------------------------------------------*/
445 gen_get_volume( struct wm_drive *d, int *left, int *right )
447 struct ioc_vol vol;
449 if (d->fd >= 0)
451 bzero((char *)&vol, sizeof(vol));
453 if (ioctl(d->fd, CDIOCGETVOL, &vol))
454 *left = *right = -1;
455 else
457 *left = unscale_volume(vol.vol[LEFT_PORT], 100);
458 *right = unscale_volume(vol.vol[RIGHT_PORT], 100);
461 else
462 *left = *right = -1;
464 return (0);
468 /*---------------------------------------------*
469 * Send an arbitrary SCSI command to a device.
471 *---------------------------------------------*/
473 wm_scsi(d, cdb, cdblen, retbuf, retbuflen, getreply)
474 struct wm_drive *d;
475 unsigned char *cdb;
476 int cdblen;
477 void *retbuf;
478 int retbuflen;
479 int getreply;
481 return (-1);
485 /*-------------------------------------------------------------------*
486 * Open the CD device and figure out what kind of drive is attached.
487 *-------------------------------------------------------------------*/
489 wmcd_open( struct wm_drive *d )
491 int fd;
492 static int warned = 0;
493 char vendor[32] = WM_STR_GENVENDOR;
494 char model[32] = WM_STR_GENMODEL;
495 char rev[32] = WM_STR_GENREV;
497 if (d->fd >= 0) /* Device already open? */
498 return (0);
500 if (cd_device == NULL)
501 cd_device = DEFAULT_CD_DEVICE;
503 d->fd = open(cd_device, 0);
504 if (d->fd < 0)
506 if (errno == EACCES)
508 if (!warned)
510 fprintf(stderr,
511 "As root, please run\n\nchmod 666 %s\n\n%s\n", cd_device,
512 "to give yourself permission to access the CD-ROM device.");
513 warned++;
517 /* No CD in drive. */
518 return (1);
521 if (warned)
523 warned = 0;
524 fprintf(stderr, "Thank you.\n");
527 /* Now fill in the relevant parts of the wm_drive structure. */
528 fd = d->fd;
530 *d = *(find_drive_struct(vendor, model, rev));
531 wm_drive_settype(vendor, model, rev);
533 (d->init)(d);
535 d->fd = fd;
537 return (0);
538 } /* wmcd_open() */
541 * Re-Open the device if it is open.
544 wmcd_reopen( struct wm_drive *d )
546 int status;
548 do {
549 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen ");
550 if (d->fd >= 0) /* Device really open? */
552 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closes the device and ");
553 status = close( d->fd ); /* close it! */
554 /* we know, that the file is closed, do we? */
555 d->fd = -1;
557 wm_susleep( 1000 );
558 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "calls wmcd_open()\n");
559 status = wmcd_open( d ); /* open it as usual */
560 wm_susleep( 1000 );
561 } while ( status != 0 );
562 return status;
563 } /* wmcd_reopen() */
565 #endif