summaryrefslogtreecommitdiffstats
path: root/drivers/char/rtrack.c
blob: efe01046abd76160c507735aa4f27fd6b89f4821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* radiotrack (radioreveal) driver for Linux radio support
 * (c) 1997 M. Kirkwood
 */
/* TODO: Allow for more than one of these foolish entities :-) */

/* Notes on the hardware (reverse engineered from other peoples'
 * reverse engineering of AIMS' code :-)
 *
 *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
 *
 *  The signal strength query is unsurprisingly inaccurate.  And it seems
 *  to indicate that (on my card, at least) the frequency setting isn't
 *  too great.  (I have to tune up .025MHz from what the freq should be
 *  to get a report that the thing is tuned.)
 *
 *  Volume control is (ugh) analogue:
 *   out(port, start_increasing_volume);
 *   wait(a_wee_while);
 *   out(port, stop_changing_the_volume);
 *  
 */

#include <linux/config.h>
#include <linux/radio.h>
#include <linux/ioport.h>

#include <linux/delay.h>
#include <asm/io.h>
#include <asm/uaccess.h>

#include "rtrack.h"

/* Let's just be a bit careful here, shall we? */
#if (CONFIG_RADIO_RTRACK_PORT != 0x20f) && (CONFIG_RADIO_RTRACK_PORT != 0x30f)
#error Invalid port specified for RadioTrack
#endif

/* local prototypes */
void outbits(int bits, int data, int port);
void decvol(int port);
void incvol(int port);
void mute(int port);
void unmute(int port);

/* public structurey-type things */
int rt_port = CONFIG_RADIO_RTRACK_PORT;

struct radio_cap rt_cap = {
	0,			/* device index (not dealt with here) */
	RADIO_TYPE_RTRACK,	/* type */
	1,			/* number of bandwidths */
	0, 10			/* volmin, volmax */
};

/* we only get one band/protocol with radiotrack */
struct radio_band rt_band = {
	0,			/* device index */
	0,			/* bandwidth "index" */
	RADIO_PROTOCOL_FM,
	RADIO_BAND_FM_STD,
	RADIO_FM_FRTOINT(88.0),	/* freq range */
	RADIO_FM_FRTOINT(108.0),
	0,1			/* sig strength range */
};

/* p'raps these should become struct radio_ops and struct radio_status? */
struct radio_device rt_dev = {
	&rt_cap,
	&rt_band,
	&rt_setvol,
	0,				/* curvol */
	&rt_setband,
	0,				/* curband */
	&rt_setfreq,
	0,				/* curfreq */
	&rt_getsigstr,
	NULL,				/* next (to be filled in later) */
	&rt_port			/* misc */
};


void radiotrack_init()
{
int dev_num, i;

/* XXX - probe here?? - XXX */

/* try to grab the i/o port */
	if(check_region(rt_port,2)) {
		printk("rtrack.c: port 0x%x already used\n", rt_port);
		return;
	}

	request_region(rt_port,2,"rtrack");

	dev_num = radio_add_device(&rt_dev);
/* initialise the card */
	/* set the volume very low */
	for(i=rt_cap.volmax; i>rt_cap.volmin; i--)
		decvol(rt_port);
	rt_dev.curvol = rt_cap.volmin;
}


int rt_setvol(struct radio_device *dev, int vol)
{
int port, i;

	if(vol == dev->curvol)
		return 0;

	port = *(int*)(dev->misc);
	if(vol == 0)
		mute(port);

	if(vol > dev->curvol)
		for(i = dev->curvol; i < vol; i++)
			incvol(port);
	else
		for(i = dev->curvol; i > vol; i--)
			decvol(port);

	if(dev->curvol == 0)
		unmute(port);

	return 0;
}


int rt_setband(struct radio_device *dev, int band)
{
/* we know in advance that we only have one band, and
 * the wrapper checks the validity of all the inputs
 */
	return 0;
}

int rt_setfreq(struct radio_device *dev, int freq)
{
int myport = *(int*)(dev->misc);

	outbits(16, RTRACK_ENCODE(freq), myport);
	outbits(8, 0xa0, myport);
/* XXX - get rid of this once setvol is implemented properly - XXX */
/* these insist on turning the thing on.  not sure I approve... */
	udelay(1000);
	outb(0, myport);
	outb(0xc8, myport);

	return 0;
}

int rt_getsigstr(struct radio_device *dev)
{
int res;
int myport = *(int*)(dev->misc);

	outb(0xf8, myport);
	udelay(200000);
	res = (int)inb(myport);
	udelay(10000);
	outb(0xe8, myport);
	if(res == 0xfd)
		return 1;
	else
		return 0;
}


/* local things */
void outbits(int bits, int data, int port)
{
	while(bits--) {
		if(data & 1) {
			outw(5, port);
			outw(5, port);
			outw(7, port);
			outw(7, port);
		} else {
			outw(1, port);
			outw(1, port);
			outw(3, port);
			outw(3, port);
		}
		data>>=1;
	}
}

void decvol(int port)
{
	outb(0x48, port);
	udelay(100000);
	outb(0xc8, port);
}

void incvol(int port)
{
	outb(0x88, port);
	udelay(100000);
	outb(0xc8, port);
}

void mute(int port)
{
	outb(0, port);
	outb(0xc0, port);
}

void unmute(int port)
{
	outb(0, port);
	outb(0xc8, port);
}