1 /* radiotrack (radioreveal) driver for Linux radio support
2  * (c) 1997 M. Kirkwood
3  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
4  * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
5  * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
6  *
7  * History:
8  * 1999-02-24	Russell Kroll <rkroll@exploits.org>
9  * 		Fine tuning/VIDEO_TUNER_LOW
10  *		Frequency range expanded to start at 87 MHz
11  *
12  * TODO: Allow for more than one of these foolish entities :-)
13  *
14  * Notes on the hardware (reverse engineered from other peoples'
15  * reverse engineering of AIMS' code :-)
16  *
17  *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
18  *
19  *  The signal strength query is unsurprisingly inaccurate.  And it seems
20  *  to indicate that (on my card, at least) the frequency setting isn't
21  *  too great.  (I have to tune up .025MHz from what the freq should be
22  *  to get a report that the thing is tuned.)
23  *
24  *  Volume control is (ugh) analogue:
25  *   out(port, start_increasing_volume);
26  *   wait(a_wee_while);
27  *   out(port, stop_changing_the_volume);
28  *
29  */
30 
31 #include <linux/module.h>	/* Modules 			*/
32 #include <linux/init.h>		/* Initdata			*/
33 #include <linux/ioport.h>	/* request_region		*/
34 #include <linux/delay.h>	/* udelay			*/
35 #include <linux/videodev2.h>	/* kernel radio structs		*/
36 #include <linux/version.h>	/* for KERNEL_VERSION MACRO	*/
37 #include <linux/io.h>		/* outb, outb_p			*/
38 #include <linux/uaccess.h>	/* copy to/from user		*/
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-ioctl.h>
41 
42 MODULE_AUTHOR("M.Kirkwood");
43 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
44 MODULE_LICENSE("GPL");
45 
46 #ifndef CONFIG_RADIO_RTRACK_PORT
47 #define CONFIG_RADIO_RTRACK_PORT -1
48 #endif
49 
50 static int io = CONFIG_RADIO_RTRACK_PORT;
51 static int radio_nr = -1;
52 
53 module_param(io, int, 0);
54 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
55 module_param(radio_nr, int, 0);
56 
57 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
58 
59 struct rtrack
60 {
61 	struct v4l2_device v4l2_dev;
62 	struct video_device vdev;
63 	int port;
64 	int curvol;
65 	unsigned long curfreq;
66 	int muted;
67 	int io;
68 	struct mutex lock;
69 };
70 
71 static struct rtrack rtrack_card;
72 
73 /* local things */
74 
75 static void sleep_delay(long n)
76 {
77 	/* Sleep nicely for 'n' uS */
78 	int d = n / msecs_to_jiffies(1000);
79 	if (!d)
80 		udelay(n);
81 	else
82 		msleep(jiffies_to_msecs(d));
83 }
84 
85 static void rt_decvol(struct rtrack *rt)
86 {
87 	outb(0x58, rt->io);		/* volume down + sigstr + on	*/
88 	sleep_delay(100000);
89 	outb(0xd8, rt->io);		/* volume steady + sigstr + on	*/
90 }
91 
92 static void rt_incvol(struct rtrack *rt)
93 {
94 	outb(0x98, rt->io);		/* volume up + sigstr + on	*/
95 	sleep_delay(100000);
96 	outb(0xd8, rt->io);		/* volume steady + sigstr + on	*/
97 }
98 
99 static void rt_mute(struct rtrack *rt)
100 {
101 	rt->muted = 1;
102 	mutex_lock(&rt->lock);
103 	outb(0xd0, rt->io);		/* volume steady, off		*/
104 	mutex_unlock(&rt->lock);
105 }
106 
107 static int rt_setvol(struct rtrack *rt, int vol)
108 {
109 	int i;
110 
111 	mutex_lock(&rt->lock);
112 
113 	if (vol == rt->curvol) {	/* requested volume = current */
114 		if (rt->muted) {	/* user is unmuting the card  */
115 			rt->muted = 0;
116 			outb(0xd8, rt->io);	/* enable card */
117 		}
118 		mutex_unlock(&rt->lock);
119 		return 0;
120 	}
121 
122 	if (vol == 0) {			/* volume = 0 means mute the card */
123 		outb(0x48, rt->io);	/* volume down but still "on"	*/
124 		sleep_delay(2000000);	/* make sure it's totally down	*/
125 		outb(0xd0, rt->io);	/* volume steady, off		*/
126 		rt->curvol = 0;		/* track the volume state!	*/
127 		mutex_unlock(&rt->lock);
128 		return 0;
129 	}
130 
131 	rt->muted = 0;
132 	if (vol > rt->curvol)
133 		for (i = rt->curvol; i < vol; i++)
134 			rt_incvol(rt);
135 	else
136 		for (i = rt->curvol; i > vol; i--)
137 			rt_decvol(rt);
138 
139 	rt->curvol = vol;
140 	mutex_unlock(&rt->lock);
141 	return 0;
142 }
143 
144 /* the 128+64 on these outb's is to keep the volume stable while tuning
145  * without them, the volume _will_ creep up with each frequency change
146  * and bit 4 (+16) is to keep the signal strength meter enabled
147  */
148 
149 static void send_0_byte(struct rtrack *rt)
150 {
151 	if (rt->curvol == 0 || rt->muted) {
152 		outb_p(128+64+16+  1, rt->io);   /* wr-enable + data low */
153 		outb_p(128+64+16+2+1, rt->io);   /* clock */
154 	}
155 	else {
156 		outb_p(128+64+16+8+  1, rt->io);  /* on + wr-enable + data low */
157 		outb_p(128+64+16+8+2+1, rt->io);  /* clock */
158 	}
159 	sleep_delay(1000);
160 }
161 
162 static void send_1_byte(struct rtrack *rt)
163 {
164 	if (rt->curvol == 0 || rt->muted) {
165 		outb_p(128+64+16+4  +1, rt->io);   /* wr-enable+data high */
166 		outb_p(128+64+16+4+2+1, rt->io);   /* clock */
167 	}
168 	else {
169 		outb_p(128+64+16+8+4  +1, rt->io); /* on+wr-enable+data high */
170 		outb_p(128+64+16+8+4+2+1, rt->io); /* clock */
171 	}
172 
173 	sleep_delay(1000);
174 }
175 
176 static int rt_setfreq(struct rtrack *rt, unsigned long freq)
177 {
178 	int i;
179 
180 	mutex_lock(&rt->lock);			/* Stop other ops interfering */
181 
182 	rt->curfreq = freq;
183 
184 	/* now uses VIDEO_TUNER_LOW for fine tuning */
185 
186 	freq += 171200;			/* Add 10.7 MHz IF 		*/
187 	freq /= 800;			/* Convert to 50 kHz units	*/
188 
189 	send_0_byte(rt);		/*  0: LSB of frequency		*/
190 
191 	for (i = 0; i < 13; i++)	/*   : frequency bits (1-13)	*/
192 		if (freq & (1 << i))
193 			send_1_byte(rt);
194 		else
195 			send_0_byte(rt);
196 
197 	send_0_byte(rt);		/* 14: test bit - always 0    */
198 	send_0_byte(rt);		/* 15: test bit - always 0    */
199 
200 	send_0_byte(rt);		/* 16: band data 0 - always 0 */
201 	send_0_byte(rt);		/* 17: band data 1 - always 0 */
202 	send_0_byte(rt);		/* 18: band data 2 - always 0 */
203 	send_0_byte(rt);		/* 19: time base - always 0   */
204 
205 	send_0_byte(rt);		/* 20: spacing (0 = 25 kHz)   */
206 	send_1_byte(rt);		/* 21: spacing (1 = 25 kHz)   */
207 	send_0_byte(rt);		/* 22: spacing (0 = 25 kHz)   */
208 	send_1_byte(rt);		/* 23: AM/FM (FM = 1, always) */
209 
210 	if (rt->curvol == 0 || rt->muted)
211 		outb(0xd0, rt->io);	/* volume steady + sigstr */
212 	else
213 		outb(0xd8, rt->io);	/* volume steady + sigstr + on */
214 
215 	mutex_unlock(&rt->lock);
216 
217 	return 0;
218 }
219 
220 static int rt_getsigstr(struct rtrack *rt)
221 {
222 	int sig = 1;
223 
224 	mutex_lock(&rt->lock);
225 	if (inb(rt->io) & 2)	/* bit set = no signal present	*/
226 		sig = 0;
227 	mutex_unlock(&rt->lock);
228 	return sig;
229 }
230 
231 static int vidioc_querycap(struct file *file, void  *priv,
232 					struct v4l2_capability *v)
233 {
234 	strlcpy(v->driver, "radio-aimslab", sizeof(v->driver));
235 	strlcpy(v->card, "RadioTrack", sizeof(v->card));
236 	strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
237 	v->version = RADIO_VERSION;
238 	v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
239 	return 0;
240 }
241 
242 static int vidioc_g_tuner(struct file *file, void *priv,
243 					struct v4l2_tuner *v)
244 {
245 	struct rtrack *rt = video_drvdata(file);
246 
247 	if (v->index > 0)
248 		return -EINVAL;
249 
250 	strlcpy(v->name, "FM", sizeof(v->name));
251 	v->type = V4L2_TUNER_RADIO;
252 	v->rangelow = 87 * 16000;
253 	v->rangehigh = 108 * 16000;
254 	v->rxsubchans = V4L2_TUNER_SUB_MONO;
255 	v->capability = V4L2_TUNER_CAP_LOW;
256 	v->audmode = V4L2_TUNER_MODE_MONO;
257 	v->signal = 0xffff * rt_getsigstr(rt);
258 	return 0;
259 }
260 
261 static int vidioc_s_tuner(struct file *file, void *priv,
262 					struct v4l2_tuner *v)
263 {
264 	return v->index ? -EINVAL : 0;
265 }
266 
267 static int vidioc_s_frequency(struct file *file, void *priv,
268 					struct v4l2_frequency *f)
269 {
270 	struct rtrack *rt = video_drvdata(file);
271 
272 	rt_setfreq(rt, f->frequency);
273 	return 0;
274 }
275 
276 static int vidioc_g_frequency(struct file *file, void *priv,
277 					struct v4l2_frequency *f)
278 {
279 	struct rtrack *rt = video_drvdata(file);
280 
281 	f->type = V4L2_TUNER_RADIO;
282 	f->frequency = rt->curfreq;
283 	return 0;
284 }
285 
286 static int vidioc_queryctrl(struct file *file, void *priv,
287 					struct v4l2_queryctrl *qc)
288 {
289 	switch (qc->id) {
290 	case V4L2_CID_AUDIO_MUTE:
291 		return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
292 	case V4L2_CID_AUDIO_VOLUME:
293 		return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
294 	}
295 	return -EINVAL;
296 }
297 
298 static int vidioc_g_ctrl(struct file *file, void *priv,
299 					struct v4l2_control *ctrl)
300 {
301 	struct rtrack *rt = video_drvdata(file);
302 
303 	switch (ctrl->id) {
304 	case V4L2_CID_AUDIO_MUTE:
305 		ctrl->value = rt->muted;
306 		return 0;
307 	case V4L2_CID_AUDIO_VOLUME:
308 		ctrl->value = rt->curvol;
309 		return 0;
310 	}
311 	return -EINVAL;
312 }
313 
314 static int vidioc_s_ctrl(struct file *file, void *priv,
315 					struct v4l2_control *ctrl)
316 {
317 	struct rtrack *rt = video_drvdata(file);
318 
319 	switch (ctrl->id) {
320 	case V4L2_CID_AUDIO_MUTE:
321 		if (ctrl->value)
322 			rt_mute(rt);
323 		else
324 			rt_setvol(rt, rt->curvol);
325 		return 0;
326 	case V4L2_CID_AUDIO_VOLUME:
327 		rt_setvol(rt, ctrl->value);
328 		return 0;
329 	}
330 	return -EINVAL;
331 }
332 
333 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
334 {
335 	*i = 0;
336 	return 0;
337 }
338 
339 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
340 {
341 	return i ? -EINVAL : 0;
342 }
343 
344 static int vidioc_g_audio(struct file *file, void *priv,
345 					struct v4l2_audio *a)
346 {
347 	a->index = 0;
348 	strlcpy(a->name, "Radio", sizeof(a->name));
349 	a->capability = V4L2_AUDCAP_STEREO;
350 	return 0;
351 }
352 
353 static int vidioc_s_audio(struct file *file, void *priv,
354 					struct v4l2_audio *a)
355 {
356 	return a->index ? -EINVAL : 0;
357 }
358 
359 static int rtrack_open(struct file *file)
360 {
361 	return 0;
362 }
363 
364 static int rtrack_release(struct file *file)
365 {
366 	return 0;
367 }
368 
369 static const struct v4l2_file_operations rtrack_fops = {
370 	.owner		= THIS_MODULE,
371 	.open           = rtrack_open,
372 	.release        = rtrack_release,
373 	.ioctl		= video_ioctl2,
374 };
375 
376 static const struct v4l2_ioctl_ops rtrack_ioctl_ops = {
377 	.vidioc_querycap    = vidioc_querycap,
378 	.vidioc_g_tuner     = vidioc_g_tuner,
379 	.vidioc_s_tuner     = vidioc_s_tuner,
380 	.vidioc_g_audio     = vidioc_g_audio,
381 	.vidioc_s_audio     = vidioc_s_audio,
382 	.vidioc_g_input     = vidioc_g_input,
383 	.vidioc_s_input     = vidioc_s_input,
384 	.vidioc_g_frequency = vidioc_g_frequency,
385 	.vidioc_s_frequency = vidioc_s_frequency,
386 	.vidioc_queryctrl   = vidioc_queryctrl,
387 	.vidioc_g_ctrl      = vidioc_g_ctrl,
388 	.vidioc_s_ctrl      = vidioc_s_ctrl,
389 };
390 
391 static int __init rtrack_init(void)
392 {
393 	struct rtrack *rt = &rtrack_card;
394 	struct v4l2_device *v4l2_dev = &rt->v4l2_dev;
395 	int res;
396 
397 	strlcpy(v4l2_dev->name, "rtrack", sizeof(v4l2_dev->name));
398 	rt->io = io;
399 
400 	if (rt->io == -1) {
401 		v4l2_err(v4l2_dev, "you must set an I/O address with io=0x???\n");
402 		return -EINVAL;
403 	}
404 
405 	if (!request_region(rt->io, 2, "rtrack")) {
406 		v4l2_err(v4l2_dev, "port 0x%x already in use\n", rt->io);
407 		return -EBUSY;
408 	}
409 
410 	res = v4l2_device_register(NULL, v4l2_dev);
411 	if (res < 0) {
412 		release_region(rt->io, 2);
413 		v4l2_err(v4l2_dev, "could not register v4l2_device\n");
414 		return res;
415 	}
416 
417 	strlcpy(rt->vdev.name, v4l2_dev->name, sizeof(rt->vdev.name));
418 	rt->vdev.v4l2_dev = v4l2_dev;
419 	rt->vdev.fops = &rtrack_fops;
420 	rt->vdev.ioctl_ops = &rtrack_ioctl_ops;
421 	rt->vdev.release = video_device_release_empty;
422 	video_set_drvdata(&rt->vdev, rt);
423 
424 	if (video_register_device(&rt->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
425 		v4l2_device_unregister(&rt->v4l2_dev);
426 		release_region(rt->io, 2);
427 		return -EINVAL;
428 	}
429 	v4l2_info(v4l2_dev, "AIMSlab RadioTrack/RadioReveal card driver.\n");
430 
431 	/* Set up the I/O locking */
432 
433 	mutex_init(&rt->lock);
434 
435 	/* mute card - prevents noisy bootups */
436 
437 	/* this ensures that the volume is all the way down  */
438 	outb(0x48, rt->io);		/* volume down but still "on"	*/
439 	sleep_delay(2000000);	/* make sure it's totally down	*/
440 	outb(0xc0, rt->io);		/* steady volume, mute card	*/
441 
442 	return 0;
443 }
444 
445 static void __exit rtrack_exit(void)
446 {
447 	struct rtrack *rt = &rtrack_card;
448 
449 	video_unregister_device(&rt->vdev);
450 	v4l2_device_unregister(&rt->v4l2_dev);
451 	release_region(rt->io, 2);
452 }
453 
454 module_init(rtrack_init);
455 module_exit(rtrack_exit);
456 
457