radio-sf16fmr2.c 11.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* SF16FMR2 radio driver for Linux radio support
 * heavily based on fmi driver...
 * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
 *
 * Notes on the hardware
 *
 *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
 *  No volume control - only mute/unmute - you have to use line volume
 *
 *  For read stereo/mono you must wait 0.1 sec after set frequency and
 *  card unmuted so I set frequency on unmute
 *  Signal handling seem to work only on autoscanning (not implemented)
13 14
 *
 *  Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
L
Linus Torvalds 已提交
15 16 17 18
 */

#include <linux/module.h>	/* Modules 			*/
#include <linux/init.h>		/* Initdata			*/
19
#include <linux/ioport.h>	/* request_region		*/
L
Linus Torvalds 已提交
20 21 22
#include <linux/delay.h>	/* udelay			*/
#include <asm/io.h>		/* outb, outb_p			*/
#include <asm/uaccess.h>	/* copy to/from user		*/
23
#include <linux/videodev2.h>	/* kernel radio structs		*/
24
#include <media/v4l2-common.h>
25
#include <media/v4l2-ioctl.h>
26
#include <linux/mutex.h>
L
Linus Torvalds 已提交
27

28 29 30
static struct mutex lock;

#include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
31 32
#define RADIO_VERSION KERNEL_VERSION(0,0,2)

33 34
#define AUD_VOL_INDEX 1

35 36 37 38 39 40 41 42
static struct v4l2_queryctrl radio_qctrl[] = {
	{
		.id            = V4L2_CID_AUDIO_MUTE,
		.name          = "Mute",
		.minimum       = 0,
		.maximum       = 1,
		.default_value = 1,
		.type          = V4L2_CTRL_TYPE_BOOLEAN,
43 44
	},
	[AUD_VOL_INDEX] = {
45 46 47
		.id            = V4L2_CID_AUDIO_VOLUME,
		.name          = "Volume",
		.minimum       = 0,
48 49 50
		.maximum       = 15,
		.step          = 1,
		.default_value = 0,
51 52 53 54
		.type          = V4L2_CTRL_TYPE_INTEGER,
	}
};

L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62 63 64 65 66
#undef DEBUG
//#define DEBUG 1

#ifdef DEBUG
# define  debug_print(s) printk s
#else
# define  debug_print(s)
#endif

/* this should be static vars for module size */
struct fmr2_device
{
67
	unsigned long in_use;
L
Linus Torvalds 已提交
68
	int port;
69
	int curvol; /* 0-15 */
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80
	int mute;
	int stereo; /* card is producing stereo audio */
	unsigned long curfreq; /* freq in kHz */
	int card_type;
	__u32 flags;
};

static int io = 0x384;
static int radio_nr = -1;

/* hw precision is 12.5 kHz
A
Adrian Bunk 已提交
81
 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
L
Linus Torvalds 已提交
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
 * other bits will be truncated
 */
#define RSF16_ENCODE(x)	((x)/200+856)
#define RSF16_MINFREQ 87*16000
#define RSF16_MAXFREQ 108*16000

static inline void wait(int n,int port)
{
	for (;n;--n) inb(port);
}

static void outbits(int bits, unsigned int data, int nWait, int port)
{
	int bit;
	for(;--bits>=0;) {
		bit = (data>>bits) & 1;
		outb(bit,port);
		wait(nWait,port);
		outb(bit|2,port);
		wait(nWait,port);
		outb(bit,port);
		wait(nWait,port);
	}
}

static inline void fmr2_mute(int port)
{
	outb(0x00, port);
	wait(4,port);
}

static inline void fmr2_unmute(int port)
{
	outb(0x04, port);
	wait(4,port);
}

static inline int fmr2_stereo_mode(int port)
{
	int n = inb(port);
	outb(6,port);
	inb(port);
	n = ((n>>3)&1)^1;
	debug_print((KERN_DEBUG "stereo: %d\n", n));
	return n;
}

static int fmr2_product_info(struct fmr2_device *dev)
{
	int n = inb(dev->port);
	n &= 0xC1;
	if (n == 0)
	{
		/* this should support volume set */
		dev->card_type = 12;
		return 0;
	}
	/* not volume (mine is 11) */
	dev->card_type = (n==128)?11:0;
	return n;
}

static inline int fmr2_getsigstr(struct fmr2_device *dev)
{
	/* !!! work only if scanning freq */
	int port = dev->port, res = 0xffff;
	outb(5,port);
	wait(4,port);
	if (!(inb(port)&1)) res = 0;
	debug_print((KERN_DEBUG "signal: %d\n", res));
	return res;
}

/* set frequency and unmute card */
static int fmr2_setfreq(struct fmr2_device *dev)
{
	int port = dev->port;
	unsigned long freq = dev->curfreq;

	fmr2_mute(port);

	/* 0x42 for mono output
	 * 0x102 forward scanning
	 * 0x182 scansione avanti
	 */
	outbits(9,0x2,3,port);
	outbits(16,RSF16_ENCODE(freq),2,port);

	fmr2_unmute(port);

	/* wait 0.11 sec */
	msleep(110);

	/* NOTE if mute this stop radio
	   you must set freq on unmute */
	dev->stereo = fmr2_stereo_mode(port);
	return 0;
}

/* !!! not tested, in my card this does't work !!! */
static int fmr2_setvolume(struct fmr2_device *dev)
{
184 185 186 187 188 189
	int vol[16] = { 0x021, 0x084, 0x090, 0x104,
			0x110, 0x204, 0x210, 0x402,
			0x404, 0x408, 0x410, 0x801,
			0x802, 0x804, 0x808, 0x810 };
	int i, a, port = dev->port;
	int n = vol[dev->curvol & 0x0f];
L
Linus Torvalds 已提交
190

191 192
	if (dev->card_type != 11)
		return 1;
L
Linus Torvalds 已提交
193

194
	for (i = 12; --i >= 0; ) {
L
Linus Torvalds 已提交
195
		a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
196 197 198 199 200 201
		outb(a | 4, port);
		wait(4, port);
		outb(a | 0x24, port);
		wait(4, port);
		outb(a | 4, port);
		wait(4, port);
L
Linus Torvalds 已提交
202
	}
203
	for (i = 6; --i >= 0; ) {
L
Linus Torvalds 已提交
204
		a = ((0x18 >> i) & 1) << 6;
205
		outb(a | 4, port);
L
Linus Torvalds 已提交
206
		wait(4,port);
207
		outb(a | 0x24, port);
L
Linus Torvalds 已提交
208 209 210 211
		wait(4,port);
		outb(a|4, port);
		wait(4,port);
	}
212
	wait(4, port);
L
Linus Torvalds 已提交
213 214 215 216 217
	outb(0x14, port);

	return 0;
}

218 219
static int vidioc_querycap(struct file *file, void  *priv,
					struct v4l2_capability *v)
L
Linus Torvalds 已提交
220
{
221 222 223 224 225 226 227 228 229 230 231 232
	strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
	strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
	sprintf(v->bus_info, "ISA");
	v->version = RADIO_VERSION;
	v->capabilities = V4L2_CAP_TUNER;
	return 0;
}

static int vidioc_g_tuner(struct file *file, void *priv,
					struct v4l2_tuner *v)
{
	int mult;
233
	struct fmr2_device *fmr2 = video_drvdata(file);
L
Linus Torvalds 已提交
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	if (v->index > 0)
		return -EINVAL;

	strcpy(v->name, "FM");
	v->type = V4L2_TUNER_RADIO;

	mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
	v->rangelow = RSF16_MINFREQ/mult;
	v->rangehigh = RSF16_MAXFREQ/mult;
	v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
	v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW;
	v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
				V4L2_TUNER_MODE_MONO;
	mutex_lock(&lock);
	v->signal = fmr2_getsigstr(fmr2);
	mutex_unlock(&lock);
	return 0;
}
253

254 255 256 257 258 259 260
static int vidioc_s_tuner(struct file *file, void *priv,
					struct v4l2_tuner *v)
{
	if (v->index > 0)
		return -EINVAL;
	return 0;
}
261

262 263 264
static int vidioc_s_frequency(struct file *file, void *priv,
					struct v4l2_frequency *f)
{
265
	struct fmr2_device *fmr2 = video_drvdata(file);
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
		f->frequency *= 1000;
	if (f->frequency < RSF16_MINFREQ ||
			f->frequency > RSF16_MAXFREQ )
		return -EINVAL;
	/*rounding in steps of 200 to match th freq
	that will be used */
	fmr2->curfreq = (f->frequency/200)*200;

	/* set card freq (if not muted) */
	if (fmr2->curvol && !fmr2->mute) {
		mutex_lock(&lock);
		fmr2_setfreq(fmr2);
		mutex_unlock(&lock);
	}
	return 0;
}
284

285 286 287
static int vidioc_g_frequency(struct file *file, void *priv,
					struct v4l2_frequency *f)
{
288
	struct fmr2_device *fmr2 = video_drvdata(file);
289

290 291 292 293 294 295
	f->type = V4L2_TUNER_RADIO;
	f->frequency = fmr2->curfreq;
	if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
		f->frequency /= 1000;
	return 0;
}
296

297 298 299 300
static int vidioc_queryctrl(struct file *file, void *priv,
					struct v4l2_queryctrl *qc)
{
	int i;
301

302 303
	for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
		if (qc->id && qc->id == radio_qctrl[i].id) {
304
			memcpy(qc, &radio_qctrl[i], sizeof(*qc));
L
Linus Torvalds 已提交
305 306
			return 0;
		}
307 308 309 310 311 312 313
	}
	return -EINVAL;
}

static int vidioc_g_ctrl(struct file *file, void *priv,
					struct v4l2_control *ctrl)
{
314
	struct fmr2_device *fmr2 = video_drvdata(file);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		ctrl->value = fmr2->mute;
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
		ctrl->value = fmr2->curvol;
		return 0;
	}
	return -EINVAL;
}

static int vidioc_s_ctrl(struct file *file, void *priv,
					struct v4l2_control *ctrl)
{
330
	struct fmr2_device *fmr2 = video_drvdata(file);
331 332 333 334 335 336

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		fmr2->mute = ctrl->value;
		break;
	case V4L2_CID_AUDIO_VOLUME:
337 338 339 340 341
		if (ctrl->value > radio_qctrl[AUD_VOL_INDEX].maximum)
			fmr2->curvol = radio_qctrl[AUD_VOL_INDEX].maximum;
		else
			fmr2->curvol = ctrl->value;

342 343 344 345 346
		break;
	default:
		return -EINVAL;
	}

L
Linus Torvalds 已提交
347
#ifdef DEBUG
348 349 350 351
	if (fmr2->curvol && !fmr2->mute)
		printk(KERN_DEBUG "unmute\n");
	else
		printk(KERN_DEBUG "mute\n");
L
Linus Torvalds 已提交
352
#endif
353

354 355 356
	mutex_lock(&lock);
	if (fmr2->curvol && !fmr2->mute) {
		fmr2_setvolume(fmr2);
357
		/* Set frequency and unmute card */
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
		fmr2_setfreq(fmr2);
	} else
		fmr2_mute(fmr2->port);
	mutex_unlock(&lock);
	return 0;
}

static int vidioc_g_audio(struct file *file, void *priv,
					struct v4l2_audio *a)
{
	if (a->index > 1)
		return -EINVAL;

	strcpy(a->name, "Radio");
	a->capability = V4L2_AUDCAP_STEREO;
	return 0;
}

static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
{
	*i = 0;
	return 0;
L
Linus Torvalds 已提交
380 381
}

382 383 384 385 386 387 388 389 390 391 392 393 394
static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
{
	if (i != 0)
		return -EINVAL;
	return 0;
}

static int vidioc_s_audio(struct file *file, void *priv,
					struct v4l2_audio *a)
{
	if (a->index != 0)
		return -EINVAL;
	return 0;
L
Linus Torvalds 已提交
395 396 397 398
}

static struct fmr2_device fmr2_unit;

399 400 401 402 403 404 405 406 407 408 409
static int fmr2_exclusive_open(struct inode *inode, struct file *file)
{
	return test_and_set_bit(0, &fmr2_unit.in_use) ? -EBUSY : 0;
}

static int fmr2_exclusive_release(struct inode *inode, struct file *file)
{
	clear_bit(0, &fmr2_unit.in_use);
	return 0;
}

410
static const struct file_operations fmr2_fops = {
L
Linus Torvalds 已提交
411
	.owner          = THIS_MODULE,
412 413
	.open           = fmr2_exclusive_open,
	.release        = fmr2_exclusive_release,
414
	.ioctl          = video_ioctl2,
415
#ifdef CONFIG_COMPAT
416
	.compat_ioctl	= v4l_compat_ioctl32,
417
#endif
L
Linus Torvalds 已提交
418 419 420
	.llseek         = no_llseek,
};

421
static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
422 423 424 425 426 427 428 429 430 431 432 433
	.vidioc_querycap    = vidioc_querycap,
	.vidioc_g_tuner     = vidioc_g_tuner,
	.vidioc_s_tuner     = vidioc_s_tuner,
	.vidioc_g_audio     = vidioc_g_audio,
	.vidioc_s_audio     = vidioc_s_audio,
	.vidioc_g_input     = vidioc_g_input,
	.vidioc_s_input     = vidioc_s_input,
	.vidioc_g_frequency = vidioc_g_frequency,
	.vidioc_s_frequency = vidioc_s_frequency,
	.vidioc_queryctrl   = vidioc_queryctrl,
	.vidioc_g_ctrl      = vidioc_g_ctrl,
	.vidioc_s_ctrl      = vidioc_s_ctrl,
L
Linus Torvalds 已提交
434 435
};

436 437 438 439
static struct video_device fmr2_radio = {
	.name		= "SF16FMR2 radio",
	.fops		= &fmr2_fops,
	.ioctl_ops 	= &fmr2_ioctl_ops,
440
	.release	= video_device_release_empty,
441 442
};

L
Linus Torvalds 已提交
443 444 445 446 447 448 449
static int __init fmr2_init(void)
{
	fmr2_unit.port = io;
	fmr2_unit.curvol = 0;
	fmr2_unit.mute = 0;
	fmr2_unit.curfreq = 0;
	fmr2_unit.stereo = 1;
450
	fmr2_unit.flags = V4L2_TUNER_CAP_LOW;
L
Linus Torvalds 已提交
451
	fmr2_unit.card_type = 0;
452
	video_set_drvdata(&fmr2_radio, &fmr2_unit);
L
Linus Torvalds 已提交
453

454
	mutex_init(&lock);
L
Linus Torvalds 已提交
455

456 457
	if (!request_region(io, 2, "sf16fmr2")) {
		printk(KERN_ERR "radio-sf16fmr2: request_region failed!\n");
L
Linus Torvalds 已提交
458 459 460
		return -EBUSY;
	}

461
	if (video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
L
Linus Torvalds 已提交
462 463 464 465 466 467
		release_region(io, 2);
		return -EINVAL;
	}

	printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
	/* mute card - prevents noisy bootups */
468
	mutex_lock(&lock);
L
Linus Torvalds 已提交
469 470
	fmr2_mute(io);
	fmr2_product_info(&fmr2_unit);
471
	mutex_unlock(&lock);
L
Linus Torvalds 已提交
472
	debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
473 474 475 476 477

	/* Only card_type == 11 implements volume */
	if (fmr2_unit.card_type != 11)
		radio_qctrl[AUD_VOL_INDEX].maximum = 1;

L
Linus Torvalds 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	return 0;
}

MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
MODULE_LICENSE("GPL");

module_param(io, int, 0);
MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
module_param(radio_nr, int, 0);

static void __exit fmr2_cleanup_module(void)
{
	video_unregister_device(&fmr2_radio);
	release_region(io,2);
}

module_init(fmr2_init);
module_exit(fmr2_cleanup_module);

#ifndef MODULE

static int __init fmr2_setup_io(char *str)
{
	get_option(&str, &io);
	return 1;
}

__setup("sf16fmr2=", fmr2_setup_io);

#endif