1 /*
2  * $Id: plat_freebsd.c,v 1.8 1999/03/07 08:36:40 dirk Exp $
3  *
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
9  *
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.
14  *
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.
19  *
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
23  *
24  *
25  * plat_freebsd.c
26  *
27  * FreeBSD-specific drive control routines.
28  *
29  * Todd Pfaff, 3/20/94
30  *
31  */
32 
33 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
34 
35 static char freebsd_id[] = "$Id: plat_freebsd.c,v 1.8 1999/03/07 08:36:40 dirk Exp $";
36 
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>
46 
47 #include "include/wm_config.h"
48 
49 #include <sys/time.h>
50 #include <sys/ioctl.h>
51 #include <sys/cdio.h>
52 
53 #if defined(__NetBSD__)
54 # define MSF_MINUTES 1
55 # define MSF_SECONDS 2
56 # define MSF_FRAMES 3
57 # include "/sys/scsi/scsi_all.h"
58 # include "/sys/scsi/scsi_cd.h"
59 #else
60 # define LEFT_PORT 0
61 # define RIGHT_PORT 1
62 #endif
63 
64 #include "include/wm_struct.h"
65 #include "include/wm_platform.h"
66 #include "include/wm_cdrom.h"
67 #include "include/wm_scsi.h"
68 #include "include/wm_helpers.h"
69 
70 #define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
71 
72 void *malloc();
73 
74 int	min_volume = 10;
75 int	max_volume = 255;
76 
77 extern char	*cd_device;
78 
79 
80 /*--------------------------------------------------------*
81  * Initialize the drive.  A no-op for the generic driver.
82  *--------------------------------------------------------*/
83 int
gen_init(struct wm_drive * d)84 gen_init(struct wm_drive *d)
85 {
86 	return (0);
87 }
88 
89 /*-------------------------------------*
90  * Get the number of tracks on the CD.
91  *-------------------------------------*/
92 int
gen_get_trackcount(struct wm_drive * d,int * tracks)93 gen_get_trackcount(struct wm_drive *d, int *tracks)
94 {
95 	struct ioc_toc_header	hdr;
96 
97 	if (ioctl(d->fd, CDIOREADTOCHEADER, &hdr) == -1)
98 		return (-1);
99 
100 	*tracks = hdr.ending_track - hdr.starting_track + 1;
101 
102 	return (0);
103 }
104 
105 /*-----------------------------------------------------------------------*
106  * Get the start time and mode (data or audio) of a track.
107  *
108  * XXX - this should get cached, but that means keeping track of ejects.
109  *-----------------------------------------------------------------------*/
110 int
gen_get_trackinfo(struct wm_drive * d,int track,int * data,int * startframe)111 gen_get_trackinfo(struct wm_drive *d, int track, int *data, int *startframe)
112 {
113 	struct ioc_read_toc_entry	toc;
114 	struct cd_toc_entry		toc_buffer;
115 
116 	bzero((char *)&toc_buffer, sizeof(toc_buffer));
117 	toc.address_format = CD_MSF_FORMAT;
118 	toc.starting_track = track;
119 	toc.data_len = sizeof(toc_buffer);
120 	toc.data = &toc_buffer;
121 
122 	if (ioctl(d->fd, CDIOREADTOCENTRYS, &toc))
123 		return (-1);
124 
125 	*data = ((toc_buffer.control & 0x4) != 0);
126 
127 #ifdef __NetBSD__
128 	*startframe = toc_buffer.addr[MSF_MINUTES]*60*75 +
129 		toc_buffer.addr[MSF_SECONDS] * 75 +
130 		toc_buffer.addr[MSF_FRAMES];
131 #else
132 	*startframe = toc_buffer.addr.msf.minute*60*75 +
133 		toc_buffer.addr.msf.second * 75 +
134 		toc_buffer.addr.msf.frame;
135 #endif
136 
137 	return (0);
138 }
139 
140 /*-------------------------------------*
141  * Get the number of frames on the CD.
142  *-------------------------------------*/
143 int
gen_get_cdlen(struct wm_drive * d,int * frames)144 gen_get_cdlen(struct wm_drive *d, int *frames)
145 {
146 	int		tmp;
147 	struct ioc_toc_header		hdr;
148 	int status;
149 
150 #define LEADOUT 0xaa			/* see scsi.c.  what a hack! */
151 	return gen_get_trackinfo(d, LEADOUT, &tmp, frames);
152 }
153 
154 /*--------------------------------------------------------------------------*
155  * Get the current status of the drive: the current play mode, the absolute
156  * position from start of disc (in frames), and the current track and index
157  * numbers if the CD is playing or paused.
158  *--------------------------------------------------------------------------*/
159 int
gen_get_drive_status(struct wm_drive * d,enum wm_cd_modes oldmode,enum wm_cd_modes * mode,int * pos,int * track,int * index)160 gen_get_drive_status(struct wm_drive *d, enum wm_cd_modes oldmode,
161                      enum wm_cd_modes *mode, int *pos, int *track, int *index)
162 {
163 	struct ioc_read_subchannel	sc;
164 	struct cd_sub_channel_info	scd;
165 
166 	/* If we can't get status, the CD is ejected, so default to that. */
167 	*mode = WM_CDM_EJECTED;
168 
169 	sc.address_format	= CD_MSF_FORMAT;
170 	sc.data_format		= CD_CURRENT_POSITION;
171 	sc.track		= 0;
172 	sc.data_len		= sizeof(scd);
173 	sc.data			= (struct cd_sub_channel_info *)&scd;
174 
175 	/* Is the device open? */
176 	if (d->fd < 0)
177 	{
178 		switch (wmcd_open(d)) {
179 		case -1:	/* error */
180 			return (-1);
181 
182 		case 1:		/* retry */
183 			return (0);
184 		}
185 	}
186 
187 	if (ioctl(d->fd, CDIOCREADSUBCHANNEL, &sc)) {
188 /*
189  * #ifdef __NetBSD__
190  *
191  * Denis Bourez <denis@rsn.fdn.fr> told me, that closing the
192  * device is mandatory for FreeBSD, too.
193  */
194 	    /* we need to release the device so the kernel will notice
195 	       reloaded media */
196 	    (void) close(d->fd);
197 	    d->fd = -1;
198 /*
199  * #endif
200  */
201 		return (0);	/* ejected */
202 	}
203 
204 	switch (scd.header.audio_status) {
205 	case CD_AS_PLAY_IN_PROGRESS:
206 		*mode = WM_CDM_PLAYING;
207 dopos:
208 #ifdef __NetBSD__
209 		*pos = scd.what.position.absaddr[MSF_MINUTES] * 60 * 75 +
210 			scd.what.position.absaddr[MSF_SECONDS] * 75 +
211 			scd.what.position.absaddr[MSF_FRAMES];
212 #else
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 #endif
217 		*track = scd.what.position.track_number;
218 		*index = scd.what.position.index_number;
219 		break;
220 
221 	case CD_AS_PLAY_PAUSED:
222 		if (oldmode == WM_CDM_PLAYING || oldmode == WM_CDM_PAUSED)
223 		{
224 			*mode = WM_CDM_PAUSED;
225 			goto dopos;
226 		}
227 		else
228 			*mode = WM_CDM_STOPPED;
229 		break;
230 
231 	case CD_AS_PLAY_COMPLETED:
232 		*mode = WM_CDM_TRACK_DONE; /* waiting for next track. */
233 		break;
234 
235 	case CD_AS_NO_STATUS:
236 	case 0:
237 		*mode = WM_CDM_STOPPED;
238 		break;
239 	}
240 
241 	return (0);
242 }
243 
244 /*---------------------------------------------------------------------------*
245  * scale_volume(vol, max)
246  *
247  * Return a volume value suitable for passing to the CD-ROM drive.  "vol"
248  * is a volume slider setting; "max" is the slider's maximum value.
249  *
250  * On Sun and DEC CD-ROM drives, the amount of sound coming out the jack
251  * increases much faster toward the top end of the volume scale than it
252  * does at the bottom.  To make up for this, we make the volume scale look
253  * sort of logarithmic (actually an upside-down inverse square curve) so
254  * that the volume value passed to the drive changes less and less as you
255  * approach the maximum slider setting.  The actual formula looks like
256  *
257  *     (max^2 - (max - vol)^2) * (max_volume - min_volume)
258  * v = --------------------------------------------------- + min_volume
259  *                           max^2
260  *
261  * If your system's volume settings aren't broken in this way, something
262  * like the following should work:
263  *
264  *	return ((vol * (max_volume - min_volume)) / max + min_volume);
265  *---------------------------------------------------------------------------*/
266 static int
scale_volume(int vol,int max)267 scale_volume(int vol, int max)
268 {
269     return ((vol * (max_volume - min_volume)) / max + min_volume);
270 }
271 
272 /*---------------------------------------------------------------------*
273  * Set the volume level for the left and right channels.  Their values
274  * range from 0 to 100.
275  *---------------------------------------------------------------------*/
276 int
gen_set_volume(struct wm_drive * d,int left,int right)277 gen_set_volume(struct wm_drive *d, int left, int right)
278 {
279 	struct ioc_vol vol;
280 
281 	if (left < 0)	/* don't laugh, I saw this happen once! */
282 	    left = 0;
283 	if (right < 0)
284 	    right = 0;
285 	left = scale_volume(left, 100);
286 	right = scale_volume(right, 100);
287 
288 	bzero((char *)&vol, sizeof(vol));
289 
290 	vol.vol[LEFT_PORT] = left;
291 	vol.vol[RIGHT_PORT] = right;
292 
293 	if (ioctl(d->fd, CDIOCSETVOL, &vol))
294 		return (-1);
295 
296 	return (0);
297 }
298 
299 /*---------------*
300  * Pause the CD.
301  *---------------*/
302 int
gen_pause(struct wm_drive * d)303 gen_pause( struct wm_drive *d )
304 {
305 	return (ioctl(d->fd, CDIOCPAUSE));
306 }
307 
308 /*-------------------------------------------------*
309  * Resume playing the CD (assuming it was paused.)
310  *-------------------------------------------------*/
311 int
gen_resume(struct wm_drive * d)312 gen_resume( struct wm_drive *d )
313 {
314 	return (ioctl(d->fd, CDIOCRESUME));
315 }
316 
317 /*--------------*
318  * Stop the CD.
319  *--------------*/
320 int
gen_stop(struct wm_drive * d)321 gen_stop( struct wm_drive *d)
322 {
323 	return (ioctl(d->fd, CDIOCSTOP));
324 }
325 
326 /*------------------------------------------------------------*
327  * Play the CD from one position to another (both in frames.)
328  *------------------------------------------------------------*/
329 int
gen_play(struct wm_drive * d,int start,int end)330 gen_play(struct wm_drive *d, int start, int end )
331 {
332 	struct ioc_play_msf	msf;
333 
334 	msf.start_m	= start / (60*75);
335 	msf.start_s	= (start % (60*75)) / 75;
336 	msf.start_f	= start % 75;
337 	msf.end_m	= end / (60*75);
338 	msf.end_s	= (end % (60*75)) / 75;
339 	msf.end_f	= end % 75;
340 
341 	if (ioctl(d->fd, CDIOCSTART))
342 		return (-1);
343 
344 	if (ioctl(d->fd, CDIOCPLAYMSF, &msf))
345 		return (-2);
346 
347 	return (0);
348 }
349 
350 /*----------------------------------------*
351  * Eject the current CD, if there is one.
352  *----------------------------------------*/
353 int
gen_eject(struct wm_drive * d)354 gen_eject( struct wm_drive *d )
355 {
356 	/* On some systems, we can check to see if the CD is mounted. */
357 	struct stat	stbuf;
358 	struct statfs	buf;
359 	int rval;
360 
361 	if (fstat(d->fd, &stbuf) != 0)
362 		return (-2);
363 
364 	/* Is this a mounted filesystem? */
365 	if (fstatfs(stbuf.st_rdev, &buf) == 0)
366 		return (-3);
367 
368 	rval = ioctl(d->fd, CDIOCALLOW);
369 
370 	if (rval == 0)
371 	    rval = ioctl(d->fd, CDIOCEJECT);
372 
373 	if (rval == 0)
374 	    rval = ioctl(d->fd, CDIOCPREVENT);
375 
376 	(void) close(d->fd);
377 
378 	return rval;
379 }
380 
381 /*----------------------------------------*
382  * Close the CD tray
383  *----------------------------------------*/
384 
gen_closetray(struct wm_drive * d)385 int gen_closetray(struct wm_drive *d)
386 {
387 #ifdef CAN_CLOSE
388 	if(!close(d->fd))
389 	{
390 		d->fd=-1;
391 		return(wmcd_reopen(d));
392 	} else {
393 		return(-1);
394 	}
395 #else
396 	/* Always succeed if the drive can't close */
397 	return(0);
398 #endif /* CAN_CLOSE */
399 } /* gen_closetray() */
400 
401 
402 /*---------------------------------------------------------------------------*
403  * unscale_volume(cd_vol, max)
404  *
405  * Given a value between min_volume and max_volume, return the volume slider
406  * value needed to achieve that value.
407  *
408  * Rather than perform floating-point calculations to reverse the above
409  * formula, we simply do a binary search of scale_volume()'s return values.
410  *--------------------------------------------------------------------------*/
411 static int
unscale_volume(int cd_vol,int max)412 unscale_volume( int cd_vol, int max )
413 {
414 	int	vol = 0, top = max, bot = 0, scaled;
415 
416 	while (bot <= top)
417 	{
418 		vol = (top + bot) / 2;
419 		scaled = scale_volume(vol, max);
420 		if (cd_vol == scaled)
421 			break;
422 		if (cd_vol < scaled)
423 			top = vol - 1;
424 		else
425 			bot = vol + 1;
426 	}
427 
428 	if (vol < 0)
429 		vol = 0;
430 	else if (vol > max)
431 		vol = max;
432 
433 	return (vol);
434 }
435 
436 /*---------------------------------------------------------------------*
437  * Read the initial volume from the drive, if available.  Each channel
438  * ranges from 0 to 100, with -1 indicating data not available.
439  *---------------------------------------------------------------------*/
440 int
gen_get_volume(struct wm_drive * d,int * left,int * right)441 gen_get_volume( struct wm_drive *d, int *left, int *right )
442 {
443 	struct ioc_vol vol;
444 
445 	if (d->fd >= 0)
446 	{
447 		bzero((char *)&vol, sizeof(vol));
448 
449 		if (ioctl(d->fd, CDIOCGETVOL, &vol))
450 			*left = *right = -1;
451 		else
452 		{
453 			*left = unscale_volume(vol.vol[LEFT_PORT], 100);
454 			*right = unscale_volume(vol.vol[RIGHT_PORT], 100);
455 		}
456 	}
457 	else
458 		*left = *right = -1;
459 
460 	return (0);
461 }
462 
463 
464 /*---------------------------------------------*
465  * Send an arbitrary SCSI command to a device.
466  *
467  *---------------------------------------------*/
468 int
wm_scsi(d,cdb,cdblen,retbuf,retbuflen,getreply)469 wm_scsi(d, cdb, cdblen, retbuf, retbuflen, getreply)
470 	struct wm_drive	*d;
471 	unsigned char	*cdb;
472 	int		cdblen;
473 	void		*retbuf;
474 	int		retbuflen;
475 	int		getreply;
476 {
477 	return (-1);
478 }
479 
480 
481 /*-------------------------------------------------------------------*
482  * Open the CD device and figure out what kind of drive is attached.
483  *-------------------------------------------------------------------*/
484 int
wmcd_open(struct wm_drive * d)485 wmcd_open( struct wm_drive *d )
486 {
487 	int		fd;
488 	static int	warned = 0;
489 	char vendor[32] = WM_STR_GENVENDOR;
490 	char  model[32] = WM_STR_GENMODEL;
491 	char    rev[32] = WM_STR_GENREV;
492 
493 	if (d->fd >= 0)		/* Device already open? */
494 		return (0);
495 
496 	if (cd_device == NULL)
497 		cd_device = DEFAULT_CD_DEVICE;
498 
499 	d->fd = open(cd_device, 0);
500 	if (d->fd < 0)
501 	{
502 		if (errno == EACCES)
503 		{
504 			if (!warned)
505 			{
506 				fprintf(stderr,
507 		"As root, please run\n\nchmod 666 %s\n\n%s\n", cd_device,
508 		"to give yourself permission to access the CD-ROM device.");
509 				warned++;
510 			}
511 		}
512 
513 		/* No CD in drive. */
514 		return (1);
515 	}
516 
517 	if (warned)
518 	{
519 		warned = 0;
520 		fprintf(stderr, "Thank you.\n");
521 	}
522 
523 	/* Now fill in the relevant parts of the wm_drive structure. */
524 	fd = d->fd;
525 
526 	*d = *(find_drive_struct(vendor, model, rev));
527 	wm_drive_settype(vendor, model, rev);
528 
529 	(d->init)(d);
530 
531 	d->fd = fd;
532 
533 	return (0);
534 } /* wmcd_open() */
535 
536 /*
537  * Re-Open the device if it is open.
538  */
539 int
wmcd_reopen(struct wm_drive * d)540 wmcd_reopen( struct wm_drive *d )
541 {
542 	int status;
543 
544 	do {
545         	wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen ");
546 		if (d->fd >= 0)		/* Device really open? */
547 		{
548         	      wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closes the device and ");
549 		      status = close( d->fd );   /* close it! */
550 		      /* we know, that the file is closed, do we? */
551         	      d->fd = -1;
552 		}
553 		wm_susleep( 1000 );
554         	wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "calls wmcd_open()\n");
555 		status = wmcd_open( d ); /* open it as usual */
556 		wm_susleep( 1000 );
557 	} while ( status != 0 );
558         return status;
559 } /* wmcd_reopen() */
560 
561 #endif
562