radio-gemtek.c 16.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8
/* GemTek radio card driver for Linux (C) 1998 Jonas Munsin <jmunsin@iki.fi>
 *
 * GemTek hasn't released any specs on the card, so the protocol had to
 * be reverse engineered with dosemu.
 *
 * Besides the protocol changes, this is mostly a copy of:
 *
 *    RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
9
 *
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14 15
 *    Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
 *    Converted to new API by Alan Cox <Alan.Cox@linux.org>
 *    Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
 *
 * TODO: Allow for more than one of these foolish entities :-)
 *
16
 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19 20
 */

#include <linux/module.h>	/* Modules 			*/
#include <linux/init.h>		/* Initdata			*/
21
#include <linux/ioport.h>	/* request_region		*/
Linus Torvalds's avatar
Linus Torvalds committed
22 23 24
#include <linux/delay.h>	/* udelay			*/
#include <asm/io.h>		/* outb, outb_p			*/
#include <asm/uaccess.h>	/* copy to/from user		*/
25
#include <linux/videodev2.h>	/* kernel radio structs		*/
26
#include <media/v4l2-ioctl.h>
27
#include <media/v4l2-common.h>
Linus Torvalds's avatar
Linus Torvalds committed
28 29
#include <linux/spinlock.h>

30 31 32
#include <linux/version.h>	/* for KERNEL_VERSION MACRO	*/
#define RADIO_VERSION KERNEL_VERSION(0,0,3)
#define RADIO_BANNER "GemTek Radio card driver: v0.0.3"
33

34 35 36 37 38 39 40 41 42 43 44
/*
 * Module info.
 */

MODULE_AUTHOR("Jonas Munsin, Pekka Seppnen <pexu@kapsi.fi>");
MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
MODULE_LICENSE("GPL");

/*
 * Module params.
 */
45

Linus Torvalds's avatar
Linus Torvalds committed
46 47 48
#ifndef CONFIG_RADIO_GEMTEK_PORT
#define CONFIG_RADIO_GEMTEK_PORT -1
#endif
49 50 51
#ifndef CONFIG_RADIO_GEMTEK_PROBE
#define CONFIG_RADIO_GEMTEK_PROBE 1
#endif
Linus Torvalds's avatar
Linus Torvalds committed
52

53 54 55 56 57 58 59
static int io		= CONFIG_RADIO_GEMTEK_PORT;
static int probe	= CONFIG_RADIO_GEMTEK_PROBE;
static int hardmute;
static int shutdown	= 1;
static int keepmuted	= 1;
static int initmute	= 1;
static int radio_nr	= -1;
60
static unsigned long in_use;
Linus Torvalds's avatar
Linus Torvalds committed
61

62
module_param(io, int, 0444);
63
MODULE_PARM_DESC(io, "Force I/O port for the GemTek Radio card if automatic "
64 65
	 "probing is disabled or fails. The most common I/O ports are: 0x20c "
	 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
66
	 "work for the combined sound/radiocard).");
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

module_param(probe, bool, 0444);
MODULE_PARM_DESC(probe, "Enable automatic device probing. Note: only the most "
	"common I/O ports used by the card are probed.");

module_param(hardmute, bool, 0644);
MODULE_PARM_DESC(hardmute, "Enable `hard muting' by shutting down PLL, may "
	 "reduce static noise.");

module_param(shutdown, bool, 0644);
MODULE_PARM_DESC(shutdown, "Enable shutting down PLL and muting line when "
	 "module is unloaded.");

module_param(keepmuted, bool, 0644);
MODULE_PARM_DESC(keepmuted, "Keep card muted even when frequency is changed.");

module_param(initmute, bool, 0444);
MODULE_PARM_DESC(initmute, "Mute card when module is loaded.");

module_param(radio_nr, int, 0444);

/*
 * Functions for controlling the card.
 */
#define GEMTEK_LOWFREQ	(87*16000)
#define GEMTEK_HIGHFREQ	(108*16000)

94 95 96 97 98 99 100 101
/*
 * Frequency calculation constants.  Intermediate frequency 10.52 MHz (nominal
 * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
 */
#define FSCALE		8
#define IF_OFFSET	((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
#define REF_FREQ	((unsigned int)(6.39 * 16 * (1<<FSCALE)))

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#define GEMTEK_CK		0x01	/* Clock signal			*/
#define GEMTEK_DA		0x02	/* Serial data			*/
#define GEMTEK_CE		0x04	/* Chip enable			*/
#define GEMTEK_NS		0x08	/* No signal			*/
#define GEMTEK_MT		0x10	/* Line mute			*/
#define GEMTEK_STDF_3_125_KHZ	0x01	/* Standard frequency 3.125 kHz	*/
#define GEMTEK_PLL_OFF		0x07	/* PLL off			*/

#define BU2614_BUS_SIZE	32	/* BU2614 / BU2614FS bus size		*/

#define SHORT_DELAY 5		/* usec */
#define LONG_DELAY 75		/* usec */

struct gemtek_device {
	unsigned long lastfreq;
Linus Torvalds's avatar
Linus Torvalds committed
117
	int muted;
118
	u32 bu2614data;
Linus Torvalds's avatar
Linus Torvalds committed
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
#define BU2614_FREQ_BITS 	16 /* D0..D15, Frequency data		*/
#define BU2614_PORT_BITS	3 /* P0..P2, Output port control data	*/
#define BU2614_VOID_BITS	4 /* unused 				*/
#define BU2614_FMES_BITS	1 /* CT, Frequency measurement beginning data */
#define BU2614_STDF_BITS	3 /* R0..R2, Standard frequency data	*/
#define BU2614_SWIN_BITS	1 /* S, Switch between FMIN / AMIN	*/
#define BU2614_SWAL_BITS        1 /* PS, Swallow counter division (AMIN only)*/
#define BU2614_VOID2_BITS	1 /* unused				*/
#define BU2614_FMUN_BITS	1 /* GT, Frequency measurement time & unlock */
#define BU2614_TEST_BITS	1 /* TS, Test data is input		*/

#define BU2614_FREQ_SHIFT 	0
#define BU2614_PORT_SHIFT	(BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
#define BU2614_VOID_SHIFT	(BU2614_PORT_BITS + BU2614_PORT_SHIFT)
#define BU2614_FMES_SHIFT	(BU2614_VOID_BITS + BU2614_VOID_SHIFT)
#define BU2614_STDF_SHIFT	(BU2614_FMES_BITS + BU2614_FMES_SHIFT)
#define BU2614_SWIN_SHIFT	(BU2614_STDF_BITS + BU2614_STDF_SHIFT)
#define BU2614_SWAL_SHIFT	(BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
#define BU2614_VOID2_SHIFT	(BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
#define BU2614_FMUN_SHIFT	(BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
#define BU2614_TEST_SHIFT	(BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)

#define MKMASK(field)	(((1<<BU2614_##field##_BITS) - 1) << \
			BU2614_##field##_SHIFT)
#define BU2614_PORT_MASK	MKMASK(PORT)
#define BU2614_FREQ_MASK	MKMASK(FREQ)
#define BU2614_VOID_MASK	MKMASK(VOID)
#define BU2614_FMES_MASK	MKMASK(FMES)
#define BU2614_STDF_MASK	MKMASK(STDF)
#define BU2614_SWIN_MASK	MKMASK(SWIN)
#define BU2614_SWAL_MASK	MKMASK(SWAL)
#define BU2614_VOID2_MASK	MKMASK(VOID2)
#define BU2614_FMUN_MASK	MKMASK(FMUN)
#define BU2614_TEST_MASK	MKMASK(TEST)
155 156 157 158

static struct gemtek_device gemtek_unit;

static spinlock_t lock;
Linus Torvalds's avatar
Linus Torvalds committed
159

160 161
/*
 * Set data which will be sent to BU2614FS.
Linus Torvalds's avatar
Linus Torvalds committed
162
 */
163 164
#define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
	((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
165 166 167 168 169 170 171 172

/*
 * Transmit settings to BU2614FS over GemTek IC.
 */
static void gemtek_bu2614_transmit(struct gemtek_device *dev)
{
	int i, bit, q, mute;

Linus Torvalds's avatar
Linus Torvalds committed
173
	spin_lock(&lock);
174 175 176 177 178 179 180 181

	mute = dev->muted ? GEMTEK_MT : 0x00;

	outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
	udelay(SHORT_DELAY);
	outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
	udelay(LONG_DELAY);

182 183 184 185 186 187
	for (i = 0, q = dev->bu2614data; i < 32; i++, q >>= 1) {
	    bit = (q & 1) ? GEMTEK_DA : 0;
	    outb_p(mute | GEMTEK_CE | bit, io);
	    udelay(SHORT_DELAY);
	    outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, io);
	    udelay(SHORT_DELAY);
188 189 190 191 192 193 194
	}

	outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
	udelay(SHORT_DELAY);
	outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
	udelay(LONG_DELAY);

Linus Torvalds's avatar
Linus Torvalds committed
195 196 197
	spin_unlock(&lock);
}

198
/*
199
 * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
200
 */
201
static unsigned long gemtek_convfreq(unsigned long freq)
Linus Torvalds's avatar
Linus Torvalds committed
202
{
203
	return ((freq<<FSCALE) + IF_OFFSET + REF_FREQ/2) / REF_FREQ;
204 205 206 207 208 209 210 211 212
}

/*
 * Set FM-frequency.
 */
static void gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
{

	if (keepmuted && hardmute && dev->muted)
Linus Torvalds's avatar
Linus Torvalds committed
213
		return;
214 215 216 217 218 219 220

	if (freq < GEMTEK_LOWFREQ)
		freq = GEMTEK_LOWFREQ;
	else if (freq > GEMTEK_HIGHFREQ)
		freq = GEMTEK_HIGHFREQ;

	dev->lastfreq = freq;
Linus Torvalds's avatar
Linus Torvalds committed
221
	dev->muted = 0;
222 223 224 225 226 227 228 229 230

	gemtek_bu2614_set(dev, BU2614_PORT, 0);
	gemtek_bu2614_set(dev, BU2614_FMES, 0);
	gemtek_bu2614_set(dev, BU2614_SWIN, 0);	/* FM-mode	*/
	gemtek_bu2614_set(dev, BU2614_SWAL, 0);
	gemtek_bu2614_set(dev, BU2614_FMUN, 1);	/* GT bit set	*/
	gemtek_bu2614_set(dev, BU2614_TEST, 0);

	gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
231
	gemtek_bu2614_set(dev, BU2614_FREQ, gemtek_convfreq(freq));
232 233

	gemtek_bu2614_transmit(dev);
Linus Torvalds's avatar
Linus Torvalds committed
234 235
}

236 237 238 239
/*
 * Set mute flag.
 */
static void gemtek_mute(struct gemtek_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
240
{
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	int i;
	dev->muted = 1;

	if (hardmute) {
		/* Turn off PLL, disable data output */
		gemtek_bu2614_set(dev, BU2614_PORT, 0);
		gemtek_bu2614_set(dev, BU2614_FMES, 0);	/* CT bit off	*/
		gemtek_bu2614_set(dev, BU2614_SWIN, 0);	/* FM-mode	*/
		gemtek_bu2614_set(dev, BU2614_SWAL, 0);
		gemtek_bu2614_set(dev, BU2614_FMUN, 0);	/* GT bit off	*/
		gemtek_bu2614_set(dev, BU2614_TEST, 0);
		gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_PLL_OFF);
		gemtek_bu2614_set(dev, BU2614_FREQ, 0);
		gemtek_bu2614_transmit(dev);
	} else {
		spin_lock(&lock);

		/* Read bus contents (CE, CK and DA). */
		i = inb_p(io);
		/* Write it back with mute flag set. */
		outb_p((i >> 5) | GEMTEK_MT, io);
		udelay(SHORT_DELAY);

		spin_unlock(&lock);
	}
Linus Torvalds's avatar
Linus Torvalds committed
266 267
}

268 269 270 271
/*
 * Unset mute flag.
 */
static void gemtek_unmute(struct gemtek_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
272
{
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	int i;
	dev->muted = 0;

	if (hardmute) {
		/* Turn PLL back on. */
		gemtek_setfreq(dev, dev->lastfreq);
	} else {
		spin_lock(&lock);

		i = inb_p(io);
		outb_p(i >> 5, io);
		udelay(SHORT_DELAY);

		spin_unlock(&lock);
	}
Linus Torvalds's avatar
Linus Torvalds committed
288 289
}

290 291 292 293
/*
 * Get signal strength (= stereo status).
 */
static inline int gemtek_getsigstr(void)
Linus Torvalds's avatar
Linus Torvalds committed
294
{
295 296
	return inb_p(io) & GEMTEK_NS ? 0 : 1;
}
Linus Torvalds's avatar
Linus Torvalds committed
297

298 299 300 301 302 303 304
/*
 * Check if requested card acts like GemTek Radio card.
 */
static int gemtek_verify(int port)
{
	static int verified = -1;
	int i, q;
Linus Torvalds's avatar
Linus Torvalds committed
305

306 307
	if (verified == port)
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
308 309

	spin_lock(&lock);
310

311 312 313 314 315 316
	q = inb_p(port);	/* Read bus contents before probing. */
	/* Try to turn on CE, CK and DA respectively and check if card responds
	   properly. */
	for (i = 0; i < 3; ++i) {
		outb_p(1 << i, port);
		udelay(SHORT_DELAY);
Linus Torvalds's avatar
Linus Torvalds committed
317

318 319 320 321 322 323 324
		if ((inb_p(port) & (~GEMTEK_NS)) != (0x17 | (1 << (i + 5)))) {
			spin_unlock(&lock);
			return 0;
		}
	}
	outb_p(q >> 5, port);	/* Write bus contents back. */
	udelay(SHORT_DELAY);
Linus Torvalds's avatar
Linus Torvalds committed
325 326

	spin_unlock(&lock);
327
	verified = port;
328

329
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
330 331
}

332 333 334 335
/*
 * Automatic probing for card.
 */
static int gemtek_probe(void)
Linus Torvalds's avatar
Linus Torvalds committed
336
{
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	int ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
	int i;

	if (!probe) {
		printk(KERN_INFO "Automatic device probing disabled.\n");
		return -1;
	}

	printk(KERN_INFO "Automatic device probing enabled.\n");

	for (i = 0; i < ARRAY_SIZE(ioports); ++i) {
		printk(KERN_INFO "Trying I/O port 0x%x...\n", ioports[i]);

		if (!request_region(ioports[i], 1, "gemtek-probe")) {
			printk(KERN_WARNING "I/O port 0x%x busy!\n",
			       ioports[i]);
			continue;
		}

		if (gemtek_verify(ioports[i])) {
			printk(KERN_INFO "Card found from I/O port "
			       "0x%x!\n", ioports[i]);

			release_region(ioports[i], 1);

			io = ioports[i];
			return io;
		}

		release_region(ioports[i], 1);
	}

	printk(KERN_ERR "Automatic probing failed!\n");

	return -1;
Linus Torvalds's avatar
Linus Torvalds committed
372 373
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
/*
 * Video 4 Linux stuff.
 */

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,
	}, {
		.id = V4L2_CID_AUDIO_VOLUME,
		.name = "Volume",
		.minimum = 0,
		.maximum = 65535,
		.step = 65535,
		.default_value = 0xff,
		.type = V4L2_CTRL_TYPE_INTEGER,
	}
};

397 398 399 400 401 402 403 404 405 406 407
static int gemtek_exclusive_open(struct inode *inode, struct file *file)
{
	return test_and_set_bit(0, &in_use) ? -EBUSY : 0;
}

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

408
static const struct file_operations gemtek_fops = {
409
	.owner		= THIS_MODULE,
410 411
	.open		= gemtek_exclusive_open,
	.release	= gemtek_exclusive_release,
412
	.ioctl		= video_ioctl2,
413
#ifdef CONFIG_COMPAT
414
	.compat_ioctl	= v4l_compat_ioctl32,
415
#endif
416 417 418 419 420
	.llseek		= no_llseek
};

static int vidioc_querycap(struct file *file, void *priv,
			   struct v4l2_capability *v)
421 422 423 424 425 426 427 428 429
{
	strlcpy(v->driver, "radio-gemtek", sizeof(v->driver));
	strlcpy(v->card, "GemTek", sizeof(v->card));
	sprintf(v->bus_info, "ISA");
	v->version = RADIO_VERSION;
	v->capabilities = V4L2_CAP_TUNER;
	return 0;
}

430
static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Linus Torvalds's avatar
Linus Torvalds committed
431
{
432 433
	if (v->index > 0)
		return -EINVAL;
434

435 436
	strcpy(v->name, "FM");
	v->type = V4L2_TUNER_RADIO;
437 438 439 440 441 442 443 444 445 446 447 448
	v->rangelow = GEMTEK_LOWFREQ;
	v->rangehigh = GEMTEK_HIGHFREQ;
	v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
	v->signal = 0xffff * gemtek_getsigstr();
	if (v->signal) {
		v->audmode = V4L2_TUNER_MODE_STEREO;
		v->rxsubchans = V4L2_TUNER_SUB_STEREO;
	} else {
		v->audmode = V4L2_TUNER_MODE_MONO;
		v->rxsubchans = V4L2_TUNER_SUB_MONO;
	}

449 450 451
	return 0;
}

452
static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
453 454 455 456 457
{
	if (v->index > 0)
		return -EINVAL;
	return 0;
}
458

459
static int vidioc_s_frequency(struct file *file, void *priv,
460
			      struct v4l2_frequency *f)
461 462
{
	struct video_device *dev = video_devdata(file);
463
	struct gemtek_device *rt = video_get_drvdata(dev);
464

465 466
	gemtek_setfreq(rt, f->frequency);

467 468
	return 0;
}
469

470
static int vidioc_g_frequency(struct file *file, void *priv,
471
			      struct v4l2_frequency *f)
472 473
{
	struct video_device *dev = video_devdata(file);
474
	struct gemtek_device *rt = video_get_drvdata(dev);
475

476
	f->type = V4L2_TUNER_RADIO;
477
	f->frequency = rt->lastfreq;
478 479
	return 0;
}
480

481
static int vidioc_queryctrl(struct file *file, void *priv,
482
			    struct v4l2_queryctrl *qc)
483 484
{
	int i;
485

486
	for (i = 0; i < ARRAY_SIZE(radio_qctrl); ++i) {
487
		if (qc->id && qc->id == radio_qctrl[i].id) {
488
			memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Linus Torvalds's avatar
Linus Torvalds committed
489 490
			return 0;
		}
491 492 493
	}
	return -EINVAL;
}
Linus Torvalds's avatar
Linus Torvalds committed
494

495
static int vidioc_g_ctrl(struct file *file, void *priv,
496
			 struct v4l2_control *ctrl)
497 498
{
	struct video_device *dev = video_devdata(file);
499
	struct gemtek_device *rt = video_get_drvdata(dev);
Linus Torvalds's avatar
Linus Torvalds committed
500

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		ctrl->value = rt->muted;
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
		if (rt->muted)
			ctrl->value = 0;
		else
			ctrl->value = 65535;
		return 0;
	}
	return -EINVAL;
}

static int vidioc_s_ctrl(struct file *file, void *priv,
516
			 struct v4l2_control *ctrl)
517 518
{
	struct video_device *dev = video_devdata(file);
519
	struct gemtek_device *rt = video_get_drvdata(dev);
520 521 522 523 524 525 526 527 528 529 530 531 532 533

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		if (ctrl->value)
			gemtek_mute(rt);
		else
			gemtek_unmute(rt);
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
		if (ctrl->value)
			gemtek_unmute(rt);
		else
			gemtek_mute(rt);
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
534
	}
535
	return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
536 537
}

538
static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Linus Torvalds's avatar
Linus Torvalds committed
539
{
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	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;
}

static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
{
	if (i != 0)
		return -EINVAL;
	return 0;
}

561
static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
562 563 564 565
{
	if (a->index != 0)
		return -EINVAL;
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
566 567
}

568
static const struct v4l2_ioctl_ops gemtek_ioctl_ops = {
569 570 571 572 573 574 575 576 577 578 579 580
	.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
Linus Torvalds's avatar
Linus Torvalds committed
581 582
};

583
static struct video_device gemtek_radio = {
584 585 586 587
	.name		= "GemTek Radio card",
	.fops		= &gemtek_fops,
	.ioctl_ops 	= &gemtek_ioctl_ops,
	.release	= video_device_release_empty,
588 589
};

590 591 592
/*
 * Initialization / cleanup related stuff.
 */
Linus Torvalds's avatar
Linus Torvalds committed
593

594 595 596
/*
 * Initilize card.
 */
Linus Torvalds's avatar
Linus Torvalds committed
597 598
static int __init gemtek_init(void)
{
599
	printk(KERN_INFO RADIO_BANNER "\n");
Linus Torvalds's avatar
Linus Torvalds committed
600

601
	spin_lock_init(&lock);
602

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	gemtek_probe();
	if (io) {
		if (!request_region(io, 1, "gemtek")) {
			printk(KERN_ERR "I/O port 0x%x already in use.\n", io);
			return -EBUSY;
		}

		if (!gemtek_verify(io))
			printk(KERN_WARNING "Card at I/O port 0x%x does not "
			       "respond properly, check your "
			       "configuration.\n", io);
		else
			printk(KERN_INFO "Using I/O port 0x%x.\n", io);
	} else if (probe) {
		printk(KERN_ERR "Automatic probing failed and no "
		       "fixed I/O port defined.\n");
		return -ENODEV;
	} else {
		printk(KERN_ERR "Automatic probing disabled but no fixed "
		       "I/O port defined.");
Linus Torvalds's avatar
Linus Torvalds committed
623 624 625
		return -EINVAL;
	}

626
	video_set_drvdata(&gemtek_radio, &gemtek_unit);
627

628
	if (video_register_device(&gemtek_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
629 630 631
		release_region(io, 1);
		return -EBUSY;
	}
Linus Torvalds's avatar
Linus Torvalds committed
632

633 634
	/* Set defaults */
	gemtek_unit.lastfreq = GEMTEK_LOWFREQ;
635
	gemtek_unit.bu2614data = 0;
Linus Torvalds's avatar
Linus Torvalds committed
636

637 638
	if (initmute)
		gemtek_mute(&gemtek_unit);
Linus Torvalds's avatar
Linus Torvalds committed
639 640 641 642

	return 0;
}

643 644 645 646
/*
 * Module cleanup
 */
static void __exit gemtek_exit(void)
Linus Torvalds's avatar
Linus Torvalds committed
647
{
648 649 650 651 652 653 654
	if (shutdown) {
		hardmute = 1;	/* Turn off PLL */
		gemtek_mute(&gemtek_unit);
	} else {
		printk(KERN_INFO "Module unloaded but card not muted!\n");
	}

Linus Torvalds's avatar
Linus Torvalds committed
655
	video_unregister_device(&gemtek_radio);
656
	release_region(io, 1);
Linus Torvalds's avatar
Linus Torvalds committed
657 658 659
}

module_init(gemtek_init);
660
module_exit(gemtek_exit);