radio-aztech.c 9.6 KB
Newer Older
1
/* radio-aztech.c - Aztech radio card driver for Linux 2.2
L
Linus Torvalds 已提交
2
 *
3
 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
4
 * Adapted to support the Video for Linux API by
L
Linus Torvalds 已提交
5 6 7 8
 * Russell Kroll <rkroll@exploits.org>.  Based on original tuner code by:
 *
 * Quay Ly
 * Donald Song
9
 * Jason Lewis      (jlewis@twilight.vtc.vsc.edu)
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Scott McGrath    (smcgrath@twilight.vtc.vsc.edu)
 * William McGrath  (wmcgrath@twilight.vtc.vsc.edu)
 *
 * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
 * along with more information on the card itself.
 *
 * History:
 * 1999-02-24	Russell Kroll <rkroll@exploits.org>
 *		Fine tuning/VIDEO_TUNER_LOW
 * 		Range expanded to 87-108 MHz (from 87.9-107.8)
 *
 * Notable changes from the original source:
 * - includes stripped down to the essentials
 * - for loops used as delays replaced with udelay()
 * - #defines removed, changed to static values
 * - tuning structure changed - no more character arrays, other changes
*/

#include <linux/module.h>	/* Modules 			*/
#include <linux/init.h>		/* Initdata			*/
30
#include <linux/ioport.h>	/* request_region		*/
L
Linus Torvalds 已提交
31
#include <linux/delay.h>	/* udelay			*/
32
#include <linux/videodev2.h>	/* kernel radio structs		*/
33 34 35
#include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
#include <linux/io.h>		/* outb, outb_p			*/
#include <media/v4l2-device.h>
36
#include <media/v4l2-ioctl.h>
L
Linus Torvalds 已提交
37

38 39 40
MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
MODULE_DESCRIPTION("A driver for the Aztech radio card.");
MODULE_LICENSE("GPL");
41

L
Linus Torvalds 已提交
42 43 44 45 46 47
/* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */

#ifndef CONFIG_RADIO_AZTECH_PORT
#define CONFIG_RADIO_AZTECH_PORT -1
#endif

48
static int io = CONFIG_RADIO_AZTECH_PORT;
L
Linus Torvalds 已提交
49 50 51
static int radio_nr = -1;
static int radio_wait_time = 1000;

52 53 54 55 56 57 58
module_param(io, int, 0);
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(io, "I/O address of the Aztech card (0x350 or 0x358)");

#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)

struct aztech
L
Linus Torvalds 已提交
59
{
60 61 62
	struct v4l2_device v4l2_dev;
	struct video_device vdev;
	int io;
L
Linus Torvalds 已提交
63 64 65
	int curvol;
	unsigned long curfreq;
	int stereo;
66
	struct mutex lock;
L
Linus Torvalds 已提交
67 68
};

69 70
static struct aztech aztech_card;

L
Linus Torvalds 已提交
71 72
static int volconvert(int level)
{
73 74
	level >>= 14;		/* Map 16bits down to 2 bit */
	level &= 3;
75

L
Linus Torvalds 已提交
76
	/* convert to card-friendly values */
77 78 79 80 81 82 83 84 85
	switch (level) {
	case 0:
		return 0;
	case 1:
		return 1;
	case 2:
		return 4;
	case 3:
		return 5;
L
Linus Torvalds 已提交
86 87 88 89
	}
	return 0;	/* Quieten gcc */
}

90
static void send_0_byte(struct aztech *az)
L
Linus Torvalds 已提交
91 92
{
	udelay(radio_wait_time);
93 94
	outb_p(2 + volconvert(az->curvol), az->io);
	outb_p(64 + 2 + volconvert(az->curvol), az->io);
L
Linus Torvalds 已提交
95 96
}

97
static void send_1_byte(struct aztech *az)
L
Linus Torvalds 已提交
98 99
{
	udelay (radio_wait_time);
100 101
	outb_p(128 + 2 + volconvert(az->curvol), az->io);
	outb_p(128 + 64 + 2 + volconvert(az->curvol), az->io);
L
Linus Torvalds 已提交
102 103
}

104
static int az_setvol(struct aztech *az, int vol)
L
Linus Torvalds 已提交
105
{
106 107 108
	mutex_lock(&az->lock);
	outb(volconvert(vol), az->io);
	mutex_unlock(&az->lock);
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119
	return 0;
}

/* thanks to Michael Dwyer for giving me a dose of clues in
 * the signal strength department..
 *
 * This card has a stereo bit - bit 0 set = mono, not set = stereo
 * It also has a "signal" bit - bit 1 set = bad signal, not set = good
 *
 */

120
static int az_getsigstr(struct aztech *az)
L
Linus Torvalds 已提交
121
{
122 123 124 125 126 127 128
	int sig = 1;

	mutex_lock(&az->lock);
	if (inb(az->io) & 2)	/* bit set = no signal present */
		sig = 0;
	mutex_unlock(&az->lock);
	return sig;
L
Linus Torvalds 已提交
129 130
}

131
static int az_getstereo(struct aztech *az)
L
Linus Torvalds 已提交
132
{
133 134 135 136 137 138 139
	int stereo = 1;

	mutex_lock(&az->lock);
	if (inb(az->io) & 1) 	/* bit set = mono */
		stereo = 0;
	mutex_unlock(&az->lock);
	return stereo;
L
Linus Torvalds 已提交
140 141
}

142
static int az_setfreq(struct aztech *az, unsigned long frequency)
L
Linus Torvalds 已提交
143 144 145
{
	int  i;

146 147 148
	mutex_lock(&az->lock);

	az->curfreq = frequency;
L
Linus Torvalds 已提交
149 150
	frequency += 171200;		/* Add 10.7 MHz IF		*/
	frequency /= 800;		/* Convert to 50 kHz units	*/
151

152
	send_0_byte(az);		/*  0: LSB of frequency       */
L
Linus Torvalds 已提交
153 154 155

	for (i = 0; i < 13; i++)	/*   : frequency bits (1-13)  */
		if (frequency & (1 << i))
156
			send_1_byte(az);
L
Linus Torvalds 已提交
157
		else
158
			send_0_byte(az);
L
Linus Torvalds 已提交
159

160 161 162 163 164
	send_0_byte(az);		/* 14: test bit - always 0    */
	send_0_byte(az);		/* 15: test bit - always 0    */
	send_0_byte(az);		/* 16: band data 0 - always 0 */
	if (az->stereo)		/* 17: stereo (1 to enable)   */
		send_1_byte(az);
L
Linus Torvalds 已提交
165
	else
166
		send_0_byte(az);
L
Linus Torvalds 已提交
167

168 169 170 171 172 173
	send_1_byte(az);		/* 18: band data 1 - unknown  */
	send_0_byte(az);		/* 19: time base - always 0   */
	send_0_byte(az);		/* 20: spacing (0 = 25 kHz)   */
	send_1_byte(az);		/* 21: spacing (1 = 25 kHz)   */
	send_0_byte(az);		/* 22: spacing (0 = 25 kHz)   */
	send_1_byte(az);		/* 23: AM/FM (FM = 1, always) */
L
Linus Torvalds 已提交
174 175 176

	/* latch frequency */

177 178
	udelay(radio_wait_time);
	outb_p(128 + 64 + volconvert(az->curvol), az->io);
179

180
	mutex_unlock(&az->lock);
L
Linus Torvalds 已提交
181 182 183 184

	return 0;
}

185
static int vidioc_querycap(struct file *file, void  *priv,
186 187
					struct v4l2_capability *v)
{
188 189 190
	strlcpy(v->driver, "radio-aztech", sizeof(v->driver));
	strlcpy(v->card, "Aztech Radio", sizeof(v->card));
	strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
191
	v->version = RADIO_VERSION;
192
	v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
193 194 195
	return 0;
}

196
static int vidioc_g_tuner(struct file *file, void *priv,
197
				struct v4l2_tuner *v)
L
Linus Torvalds 已提交
198
{
199
	struct aztech *az = video_drvdata(file);
200

201 202
	if (v->index > 0)
		return -EINVAL;
203

204
	strlcpy(v->name, "FM", sizeof(v->name));
205
	v->type = V4L2_TUNER_RADIO;
206

207 208 209 210 211
	v->rangelow = 87 * 16000;
	v->rangehigh = 108 * 16000;
	v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
	v->capability = V4L2_TUNER_CAP_LOW;
	if (az_getstereo(az))
212 213 214
		v->audmode = V4L2_TUNER_MODE_STEREO;
	else
		v->audmode = V4L2_TUNER_MODE_MONO;
215
	v->signal = 0xFFFF * az_getsigstr(az);
216

217 218
	return 0;
}
219

220
static int vidioc_s_tuner(struct file *file, void *priv,
221 222
				struct v4l2_tuner *v)
{
223
	return v->index ? -EINVAL : 0;
224 225
}

226 227 228 229 230 231 232 233
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)
{
234
	return i ? -EINVAL : 0;
235 236
}

237
static int vidioc_g_audio(struct file *file, void *priv,
238 239
			   struct v4l2_audio *a)
{
240 241 242
	a->index = 0;
	strlcpy(a->name, "Radio", sizeof(a->name));
	a->capability = V4L2_AUDCAP_STEREO;
243 244 245
	return 0;
}

246 247 248 249 250 251 252
static int vidioc_s_audio(struct file *file, void *priv,
			   struct v4l2_audio *a)
{
	return a->index ? -EINVAL : 0;
}

static int vidioc_s_frequency(struct file *file, void *priv,
253 254
				struct v4l2_frequency *f)
{
255
	struct aztech *az = video_drvdata(file);
256

257 258
	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
		return -EINVAL;
259
	az_setfreq(az, f->frequency);
260 261 262
	return 0;
}

263
static int vidioc_g_frequency(struct file *file, void *priv,
264 265
				struct v4l2_frequency *f)
{
266
	struct aztech *az = video_drvdata(file);
267

268 269
	if (f->tuner != 0)
		return -EINVAL;
270 271 272 273 274
	f->type = V4L2_TUNER_RADIO;
	f->frequency = az->curfreq;
	return 0;
}

275
static int vidioc_queryctrl(struct file *file, void *priv,
276 277
			    struct v4l2_queryctrl *qc)
{
278 279 280 281 282
	switch (qc->id) {
	case V4L2_CID_AUDIO_MUTE:
		return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
	case V4L2_CID_AUDIO_VOLUME:
		return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
283 284 285
	}
	return -EINVAL;
}
286

287
static int vidioc_g_ctrl(struct file *file, void *priv,
288 289
			    struct v4l2_control *ctrl)
{
290
	struct aztech *az = video_drvdata(file);
291 292

	switch (ctrl->id) {
293 294 295 296 297 298 299 300 301
	case V4L2_CID_AUDIO_MUTE:
		if (az->curvol == 0)
			ctrl->value = 1;
		else
			ctrl->value = 0;
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
		ctrl->value = az->curvol * 6554;
		return 0;
L
Linus Torvalds 已提交
302
	}
303
	return -EINVAL;
L
Linus Torvalds 已提交
304 305
}

306
static int vidioc_s_ctrl(struct file *file, void *priv,
307
			    struct v4l2_control *ctrl)
L
Linus Torvalds 已提交
308
{
309
	struct aztech *az = video_drvdata(file);
310 311

	switch (ctrl->id) {
312 313 314 315 316 317 318 319 320
	case V4L2_CID_AUDIO_MUTE:
		if (ctrl->value)
			az_setvol(az, 0);
		else
			az_setvol(az, az->curvol);
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
		az_setvol(az, ctrl->value);
		return 0;
321 322
	}
	return -EINVAL;
L
Linus Torvalds 已提交
323 324
}

325
static const struct v4l2_file_operations aztech_fops = {
L
Linus Torvalds 已提交
326
	.owner		= THIS_MODULE,
327
	.ioctl		= video_ioctl2,
L
Linus Torvalds 已提交
328 329
};

330
static const struct v4l2_ioctl_ops aztech_ioctl_ops = {
331 332 333
	.vidioc_querycap    = vidioc_querycap,
	.vidioc_g_tuner     = vidioc_g_tuner,
	.vidioc_s_tuner     = vidioc_s_tuner,
334 335
	.vidioc_g_audio     = vidioc_g_audio,
	.vidioc_s_audio     = vidioc_s_audio,
336 337
	.vidioc_g_input     = vidioc_g_input,
	.vidioc_s_input     = vidioc_s_input,
338 339 340 341 342
	.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 已提交
343 344 345 346
};

static int __init aztech_init(void)
{
347 348 349 350 351 352 353 354
	struct aztech *az = &aztech_card;
	struct v4l2_device *v4l2_dev = &az->v4l2_dev;
	int res;

	strlcpy(v4l2_dev->name, "aztech", sizeof(v4l2_dev->name));
	az->io = io;

	if (az->io == -1) {
355
		v4l2_err(v4l2_dev, "you must set an I/O address with io=0x350 or 0x358\n");
L
Linus Torvalds 已提交
356 357 358
		return -EINVAL;
	}

359 360
	if (!request_region(az->io, 2, "aztech")) {
		v4l2_err(v4l2_dev, "port 0x%x already in use\n", az->io);
L
Linus Torvalds 已提交
361 362 363
		return -EBUSY;
	}

364 365 366 367 368 369
	res = v4l2_device_register(NULL, v4l2_dev);
	if (res < 0) {
		release_region(az->io, 2);
		v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
		return res;
	}
370

371 372 373 374 375 376 377 378 379 380 381
	mutex_init(&az->lock);
	strlcpy(az->vdev.name, v4l2_dev->name, sizeof(az->vdev.name));
	az->vdev.v4l2_dev = v4l2_dev;
	az->vdev.fops = &aztech_fops;
	az->vdev.ioctl_ops = &aztech_ioctl_ops;
	az->vdev.release = video_device_release_empty;
	video_set_drvdata(&az->vdev, az);

	if (video_register_device(&az->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
		v4l2_device_unregister(v4l2_dev);
		release_region(az->io, 2);
L
Linus Torvalds 已提交
382 383
		return -EINVAL;
	}
384

385
	v4l2_info(v4l2_dev, "Aztech radio card driver v1.00/19990224 rkroll@exploits.org\n");
L
Linus Torvalds 已提交
386
	/* mute card - prevents noisy bootups */
387
	outb(0, az->io);
L
Linus Torvalds 已提交
388 389 390
	return 0;
}

391
static void __exit aztech_exit(void)
L
Linus Torvalds 已提交
392
{
393 394 395 396 397
	struct aztech *az = &aztech_card;

	video_unregister_device(&az->vdev);
	v4l2_device_unregister(&az->v4l2_dev);
	release_region(az->io, 2);
L
Linus Torvalds 已提交
398 399 400
}

module_init(aztech_init);
401
module_exit(aztech_exit);