1 /*
2  * $Id: drv_sony.c,v 1.3 1999/02/14 09:50:42 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  * Vendor-specific drive control routines for Sony CDU-8012 series.
26  */
27 
28 static char drv_sony_id[] = "$Id: drv_sony.c,v 1.3 1999/02/14 09:50:42 dirk Exp $";
29 
30 #include <stdio.h>
31 #include <errno.h>
32 #include "include/wm_config.h"
33 #include "include/wm_struct.h"
34 #include "include/wm_scsi.h"
35 
36 #define PAGE_AUDIO		0x0e
37 
38 /* local prototypes */
39 static int	sony_init( struct wm_drive *d );
40 static int	sony_set_volume(struct wm_drive *d, int left, int right );
41 static int	sony_get_volume( struct wm_drive *d, int *left, int *right );
42 
43 
44 extern int	min_volume, max_volume;
45 
46 struct wm_drive sony_proto = {
47 	-1,			/* fd */
48 	"Sony",			/* vendor */
49 	"CDU-8012",		/* model */
50 	"",			/* revision */
51 	NULL,			/* aux */
52 	NULL,			/* daux */
53 
54 	sony_init,		/* functions... */
55 	gen_get_trackcount,
56 	gen_get_cdlen,
57 	gen_get_trackinfo,
58 	gen_get_drive_status,
59 	sony_get_volume,
60 	sony_set_volume,
61 	gen_pause,
62 	gen_resume,
63 	gen_stop,
64 	gen_play,
65 	gen_eject,
66 	gen_closetray
67 };
68 
69 /*
70  * Initialize the driver.
71  */
sony_init(struct wm_drive * d)72 static int sony_init( struct wm_drive *d ) {
73 
74 /* Sun use Sony drives as well */
75 #if defined(SYSV) && defined(CODEC)
76         codec_init();
77 #endif
78 	min_volume = 128;
79 	max_volume = 255;
80 	return( 0 );
81 } /* sony_init() */
82 
83 /*
84  * On the Sony CDU-8012 drive, the amount of sound coming out the jack
85  * increases much faster toward the top end of the volume scale than it
86  * does at the bottom.  To make up for this, we make the volume scale look
87  * sort of logarithmic (actually an upside-down inverse square curve) so
88  * that the volume value passed to the drive changes less and less as you
89  * approach the maximum slider setting.  Additionally, only the top half
90  * of the volume scale is valid; the bottom half is all silent.  The actual
91  * formula looks like
92  *
93  *     max^2 - (max - vol)^2   max
94  * v = --------------------- + ---
95  *            max * 2           2
96  *
97  * Where "max" is the maximum value of the volume scale, usually 100.
98  */
99 static int
scale_volume(vol,max)100 scale_volume(vol, max)
101 	int	vol, max;
102 {
103 	vol = (max*max - (max - vol) * (max - vol)) / max;
104 	return ((vol + max) / 2);
105 }
106 
107 /*
108  * Given a value between min_volume and max_volume, return the standard-scale
109  * volume value needed to achieve that hardware value.
110  *
111  * Rather than perform floating-point calculations to reverse the above
112  * formula, we simply do a binary search of scale_volume()'s return values.
113  */
114 static int
unscale_volume(cd_vol,max)115 unscale_volume(cd_vol, max)
116 	int	cd_vol, max;
117 {
118 	int	vol = 0, top = max, bot = 0, scaled = 0;
119 
120 	cd_vol = (cd_vol * 100 + (max_volume - 1)) / max_volume;
121 
122 	while (bot <= top)
123 	{
124 		vol = (top + bot) / 2;
125 		scaled = scale_volume(vol, max);
126 		if (cd_vol <= scaled)
127 			top = vol - 1;
128 		else
129 			bot = vol + 1;
130 	}
131 
132 	/* Might have looked down too far for repeated scaled values */
133 	if (cd_vol < scaled)
134 		vol++;
135 
136 	if (vol < 0)
137 		vol = 0;
138 	else if (vol > max)
139 		vol = max;
140 
141 	return (vol);
142 }
143 
144 /*
145  * Get the volume.  Sun's CD-ROM driver doesn't support this operation, even
146  * though their drive does.  Dumb.
147  */
148 static int
sony_get_volume(struct wm_drive * d,int * left,int * right)149 sony_get_volume( struct wm_drive *d, int *left, int *right )
150 {
151 	unsigned char	mode[16];
152 
153 	/* Get the current audio parameters first. */
154 	if (wm_scsi_mode_sense(d, PAGE_AUDIO, mode))
155 		return (-1);
156 
157 	*left = unscale_volume(mode[9], 100);
158 	*right = unscale_volume(mode[11], 100);
159 
160 	return (0);
161 }
162 
163 /*
164  * Set the volume using the wacky scale outlined above.  The Sony drive
165  * responds to the standard set-volume command.
166  */
167 static int
sony_set_volume(struct wm_drive * d,int left,int right)168 sony_set_volume(struct wm_drive *d, int left, int right )
169 {
170 	left = scale_volume(left, 100);
171 	right = scale_volume(right, 100);
172 	return (gen_set_volume(d, left, right));
173 }
174