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.Cox@linux.org>
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 <asm/io.h>		/* outb, outb_p			*/
36 #include <asm/uaccess.h>	/* copy to/from user		*/
37 #include <linux/videodev2.h>	/* kernel radio structs		*/
38 #include <media/v4l2-common.h>
39 #include <asm/semaphore.h>	/* Lock for the I/O 		*/
40 
41 #include <linux/version.h>	/* for KERNEL_VERSION MACRO	*/
42 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
43 
44 #ifndef CONFIG_RADIO_RTRACK_PORT
45 #define CONFIG_RADIO_RTRACK_PORT -1
46 #endif
47 
48 static int io = CONFIG_RADIO_RTRACK_PORT;
49 static int radio_nr = -1;
50 static struct mutex lock;
51 
52 struct rt_device
53 {
54 	int port;
55 	int curvol;
56 	unsigned long curfreq;
57 	int muted;
58 };
59 
60 
61 /* local things */
62 
63 static void sleep_delay(long n)
64 {
65 	/* Sleep nicely for 'n' uS */
66 	int d=n/msecs_to_jiffies(1000);
67 	if(!d)
68 		udelay(n);
69 	else
70 		msleep(jiffies_to_msecs(d));
71 }
72 
73 static void rt_decvol(void)
74 {
75 	outb(0x58, io);		/* volume down + sigstr + on	*/
76 	sleep_delay(100000);
77 	outb(0xd8, io);		/* volume steady + sigstr + on	*/
78 }
79 
80 static void rt_incvol(void)
81 {
82 	outb(0x98, io);		/* volume up + sigstr + on	*/
83 	sleep_delay(100000);
84 	outb(0xd8, io);		/* volume steady + sigstr + on	*/
85 }
86 
87 static void rt_mute(struct rt_device *dev)
88 {
89 	dev->muted = 1;
90 	mutex_lock(&lock);
91 	outb(0xd0, io);			/* volume steady, off		*/
92 	mutex_unlock(&lock);
93 }
94 
95 static int rt_setvol(struct rt_device *dev, int vol)
96 {
97 	int i;
98 
99 	mutex_lock(&lock);
100 
101 	if(vol == dev->curvol) {	/* requested volume = current */
102 		if (dev->muted) {	/* user is unmuting the card  */
103 			dev->muted = 0;
104 			outb (0xd8, io);	/* enable card */
105 		}
106 		mutex_unlock(&lock);
107 		return 0;
108 	}
109 
110 	if(vol == 0) {			/* volume = 0 means mute the card */
111 		outb(0x48, io);		/* volume down but still "on"	*/
112 		sleep_delay(2000000);	/* make sure it's totally down	*/
113 		outb(0xd0, io);		/* volume steady, off		*/
114 		dev->curvol = 0;	/* track the volume state!	*/
115 		mutex_unlock(&lock);
116 		return 0;
117 	}
118 
119 	dev->muted = 0;
120 	if(vol > dev->curvol)
121 		for(i = dev->curvol; i < vol; i++)
122 			rt_incvol();
123 	else
124 		for(i = dev->curvol; i > vol; i--)
125 			rt_decvol();
126 
127 	dev->curvol = vol;
128 	mutex_unlock(&lock);
129 	return 0;
130 }
131 
132 /* the 128+64 on these outb's is to keep the volume stable while tuning
133  * without them, the volume _will_ creep up with each frequency change
134  * and bit 4 (+16) is to keep the signal strength meter enabled
135  */
136 
137 static void send_0_byte(int port, struct rt_device *dev)
138 {
139 	if ((dev->curvol == 0) || (dev->muted)) {
140 		outb_p(128+64+16+  1, port);   /* wr-enable + data low */
141 		outb_p(128+64+16+2+1, port);   /* clock */
142 	}
143 	else {
144 		outb_p(128+64+16+8+  1, port);  /* on + wr-enable + data low */
145 		outb_p(128+64+16+8+2+1, port);  /* clock */
146 	}
147 	sleep_delay(1000);
148 }
149 
150 static void send_1_byte(int port, struct rt_device *dev)
151 {
152 	if ((dev->curvol == 0) || (dev->muted)) {
153 		outb_p(128+64+16+4  +1, port);   /* wr-enable+data high */
154 		outb_p(128+64+16+4+2+1, port);   /* clock */
155 	}
156 	else {
157 		outb_p(128+64+16+8+4  +1, port); /* on+wr-enable+data high */
158 		outb_p(128+64+16+8+4+2+1, port); /* clock */
159 	}
160 
161 	sleep_delay(1000);
162 }
163 
164 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
165 {
166 	int i;
167 
168 	/* adapted from radio-aztech.c */
169 
170 	/* now uses VIDEO_TUNER_LOW for fine tuning */
171 
172 	freq += 171200;			/* Add 10.7 MHz IF 		*/
173 	freq /= 800;			/* Convert to 50 kHz units	*/
174 
175 	mutex_lock(&lock);			/* Stop other ops interfering */
176 
177 	send_0_byte (io, dev);		/*  0: LSB of frequency		*/
178 
179 	for (i = 0; i < 13; i++)	/*   : frequency bits (1-13)	*/
180 		if (freq & (1 << i))
181 			send_1_byte (io, dev);
182 		else
183 			send_0_byte (io, dev);
184 
185 	send_0_byte (io, dev);		/* 14: test bit - always 0    */
186 	send_0_byte (io, dev);		/* 15: test bit - always 0    */
187 
188 	send_0_byte (io, dev);		/* 16: band data 0 - always 0 */
189 	send_0_byte (io, dev);		/* 17: band data 1 - always 0 */
190 	send_0_byte (io, dev);		/* 18: band data 2 - always 0 */
191 	send_0_byte (io, dev);		/* 19: time base - always 0   */
192 
193 	send_0_byte (io, dev);		/* 20: spacing (0 = 25 kHz)   */
194 	send_1_byte (io, dev);		/* 21: spacing (1 = 25 kHz)   */
195 	send_0_byte (io, dev);		/* 22: spacing (0 = 25 kHz)   */
196 	send_1_byte (io, dev);		/* 23: AM/FM (FM = 1, always) */
197 
198 	if ((dev->curvol == 0) || (dev->muted))
199 		outb (0xd0, io);	/* volume steady + sigstr */
200 	else
201 		outb (0xd8, io);	/* volume steady + sigstr + on */
202 
203 	mutex_unlock(&lock);
204 
205 	return 0;
206 }
207 
208 static int rt_getsigstr(struct rt_device *dev)
209 {
210 	if (inb(io) & 2)	/* bit set = no signal present	*/
211 		return 0;
212 	return 1;		/* signal present		*/
213 }
214 
215 static struct v4l2_queryctrl radio_qctrl[] = {
216 	{
217 		.id            = V4L2_CID_AUDIO_MUTE,
218 		.name          = "Mute",
219 		.minimum       = 0,
220 		.maximum       = 1,
221 		.default_value = 1,
222 		.type          = V4L2_CTRL_TYPE_BOOLEAN,
223 	},{
224 		.id            = V4L2_CID_AUDIO_VOLUME,
225 		.name          = "Volume",
226 		.minimum       = 0,
227 		.maximum       = 0xff,
228 		.step          = 1,
229 		.default_value = 0xff,
230 		.type          = V4L2_CTRL_TYPE_INTEGER,
231 	}
232 };
233 
234 static int vidioc_querycap(struct file *file, void  *priv,
235 					struct v4l2_capability *v)
236 {
237 	strlcpy(v->driver, "radio-aimslab", sizeof(v->driver));
238 	strlcpy(v->card, "RadioTrack", sizeof(v->card));
239 	sprintf(v->bus_info, "ISA");
240 	v->version = RADIO_VERSION;
241 	v->capabilities = V4L2_CAP_TUNER;
242 	return 0;
243 }
244 
245 static int vidioc_g_tuner(struct file *file, void *priv,
246 					struct v4l2_tuner *v)
247 {
248 	struct video_device *dev = video_devdata(file);
249 	struct rt_device *rt = dev->priv;
250 
251 	if (v->index > 0)
252 		return -EINVAL;
253 
254 	strcpy(v->name, "FM");
255 	v->type = V4L2_TUNER_RADIO;
256 	v->rangelow = (87*16000);
257 	v->rangehigh = (108*16000);
258 	v->rxsubchans = V4L2_TUNER_SUB_MONO;
259 	v->capability = V4L2_TUNER_CAP_LOW;
260 	v->audmode = V4L2_TUNER_MODE_MONO;
261 	v->signal = 0xffff*rt_getsigstr(rt);
262 	return 0;
263 }
264 
265 static int vidioc_s_tuner(struct file *file, void *priv,
266 					struct v4l2_tuner *v)
267 {
268 	if (v->index > 0)
269 		return -EINVAL;
270 	return 0;
271 }
272 
273 static int vidioc_s_frequency(struct file *file, void *priv,
274 					struct v4l2_frequency *f)
275 {
276 	struct video_device *dev = video_devdata(file);
277 	struct rt_device *rt = dev->priv;
278 
279 	rt->curfreq = f->frequency;
280 	rt_setfreq(rt, rt->curfreq);
281 	return 0;
282 }
283 
284 static int vidioc_g_frequency(struct file *file, void *priv,
285 					struct v4l2_frequency *f)
286 {
287 	struct video_device *dev = video_devdata(file);
288 	struct rt_device *rt = dev->priv;
289 
290 	f->type = V4L2_TUNER_RADIO;
291 	f->frequency = rt->curfreq;
292 	return 0;
293 }
294 
295 static int vidioc_queryctrl(struct file *file, void *priv,
296 					struct v4l2_queryctrl *qc)
297 {
298 	int i;
299 
300 	for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
301 		if (qc->id && qc->id == radio_qctrl[i].id) {
302 			memcpy(qc, &(radio_qctrl[i]),
303 						sizeof(*qc));
304 			return 0;
305 		}
306 	}
307 	return -EINVAL;
308 }
309 
310 static int vidioc_g_ctrl(struct file *file, void *priv,
311 					struct v4l2_control *ctrl)
312 {
313 	struct video_device *dev = video_devdata(file);
314 	struct rt_device *rt = dev->priv;
315 
316 	switch (ctrl->id) {
317 	case V4L2_CID_AUDIO_MUTE:
318 		ctrl->value = rt->muted;
319 		return 0;
320 	case V4L2_CID_AUDIO_VOLUME:
321 		ctrl->value = rt->curvol * 6554;
322 		return 0;
323 	}
324 	return -EINVAL;
325 }
326 
327 static int vidioc_s_ctrl(struct file *file, void *priv,
328 					struct v4l2_control *ctrl)
329 {
330 	struct video_device *dev = video_devdata(file);
331 	struct rt_device *rt = dev->priv;
332 
333 	switch (ctrl->id) {
334 	case V4L2_CID_AUDIO_MUTE:
335 		if (ctrl->value)
336 			rt_mute(rt);
337 		else
338 			rt_setvol(rt,rt->curvol);
339 		return 0;
340 	case V4L2_CID_AUDIO_VOLUME:
341 		rt_setvol(rt,ctrl->value);
342 		return 0;
343 	}
344 	return -EINVAL;
345 }
346 
347 static int vidioc_g_audio (struct file *file, void *priv,
348 					struct v4l2_audio *a)
349 {
350 	if (a->index > 1)
351 		return -EINVAL;
352 
353 	strcpy(a->name, "Radio");
354 	a->capability = V4L2_AUDCAP_STEREO;
355 	return 0;
356 }
357 
358 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
359 {
360 	*i = 0;
361 	return 0;
362 }
363 
364 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
365 {
366 	if (i != 0)
367 		return -EINVAL;
368 	return 0;
369 }
370 
371 static int vidioc_s_audio(struct file *file, void *priv,
372 					struct v4l2_audio *a)
373 {
374 	if (a->index != 0)
375 		return -EINVAL;
376 	return 0;
377 }
378 
379 static struct rt_device rtrack_unit;
380 
381 static const struct file_operations rtrack_fops = {
382 	.owner		= THIS_MODULE,
383 	.open           = video_exclusive_open,
384 	.release        = video_exclusive_release,
385 	.ioctl		= video_ioctl2,
386 	.compat_ioctl	= v4l_compat_ioctl32,
387 	.llseek         = no_llseek,
388 };
389 
390 static struct video_device rtrack_radio=
391 {
392 	.owner		= THIS_MODULE,
393 	.name		= "RadioTrack radio",
394 	.type		= VID_TYPE_TUNER,
395 	.fops           = &rtrack_fops,
396 	.vidioc_querycap    = vidioc_querycap,
397 	.vidioc_g_tuner     = vidioc_g_tuner,
398 	.vidioc_s_tuner     = vidioc_s_tuner,
399 	.vidioc_g_audio     = vidioc_g_audio,
400 	.vidioc_s_audio     = vidioc_s_audio,
401 	.vidioc_g_input     = vidioc_g_input,
402 	.vidioc_s_input     = vidioc_s_input,
403 	.vidioc_g_frequency = vidioc_g_frequency,
404 	.vidioc_s_frequency = vidioc_s_frequency,
405 	.vidioc_queryctrl   = vidioc_queryctrl,
406 	.vidioc_g_ctrl      = vidioc_g_ctrl,
407 	.vidioc_s_ctrl      = vidioc_s_ctrl,
408 };
409 
410 static int __init rtrack_init(void)
411 {
412 	if(io==-1)
413 	{
414 		printk(KERN_ERR "You must set an I/O address with io=0x???\n");
415 		return -EINVAL;
416 	}
417 
418 	if (!request_region(io, 2, "rtrack"))
419 	{
420 		printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
421 		return -EBUSY;
422 	}
423 
424 	rtrack_radio.priv=&rtrack_unit;
425 
426 	if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
427 	{
428 		release_region(io, 2);
429 		return -EINVAL;
430 	}
431 	printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
432 
433 	/* Set up the I/O locking */
434 
435 	mutex_init(&lock);
436 
437 	/* mute card - prevents noisy bootups */
438 
439 	/* this ensures that the volume is all the way down  */
440 	outb(0x48, io);		/* volume down but still "on"	*/
441 	sleep_delay(2000000);	/* make sure it's totally down	*/
442 	outb(0xc0, io);		/* steady volume, mute card	*/
443 	rtrack_unit.curvol = 0;
444 
445 	return 0;
446 }
447 
448 MODULE_AUTHOR("M.Kirkwood");
449 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
450 MODULE_LICENSE("GPL");
451 
452 module_param(io, int, 0);
453 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
454 module_param(radio_nr, int, 0);
455 
456 static void __exit cleanup_rtrack_module(void)
457 {
458 	video_unregister_device(&rtrack_radio);
459 	release_region(io,2);
460 }
461 
462 module_init(rtrack_init);
463 module_exit(cleanup_rtrack_module);
464 
465