radio-gemtek-pci.c 11.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 ***************************************************************************
3
 *
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *     radio-gemtek-pci.c - Gemtek PCI Radio driver
 *     (C) 2001 Vladimir Shebordaev <vshebordaev@mail.ru>
 *
 ***************************************************************************
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public
 *     License along with this program; if not, write to the Free
 *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 *     USA.
 *
 ***************************************************************************
 *
 *     Gemtek Corp still silently refuses to release any specifications
 *     of their multimedia devices, so the protocol still has to be
 *     reverse engineered.
 *
 *     The v4l code was inspired by Jonas Munsin's  Gemtek serial line
 *     radio device driver.
 *
 *     Please, let me know if this piece of code was useful :)
34
 *
L
Linus Torvalds 已提交
35 36
 *     TODO: multiple device support and portability were not tested
 *
37 38
 *     Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
 *
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46
 ***************************************************************************
 */

#include <linux/types.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
47
#include <linux/videodev2.h>
L
Linus Torvalds 已提交
48
#include <linux/errno.h>
49
#include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
50 51 52
#include <linux/io.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
53

54 55 56 57 58 59 60 61 62 63 64 65 66
MODULE_AUTHOR("Vladimir Shebordaev <vshebordaev@mail.ru>");
MODULE_DESCRIPTION("The video4linux driver for the Gemtek PCI Radio Card");
MODULE_LICENSE("GPL");

static int nr_radio = -1;
static int mx = 1;

module_param(mx, bool, 0);
MODULE_PARM_DESC(mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not");
module_param(nr_radio, int, 0);
MODULE_PARM_DESC(nr_radio, "video4linux device number to use");

#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

#ifndef PCI_VENDOR_ID_GEMTEK
#define PCI_VENDOR_ID_GEMTEK 0x5046
#endif

#ifndef PCI_DEVICE_ID_GEMTEK_PR103
#define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
#endif

#ifndef GEMTEK_PCI_RANGE_LOW
#define GEMTEK_PCI_RANGE_LOW (87*16000)
#endif

#ifndef GEMTEK_PCI_RANGE_HIGH
#define GEMTEK_PCI_RANGE_HIGH (108*16000)
#endif

84 85 86 87 88
struct gemtek_pci {
	struct v4l2_device v4l2_dev;
	struct video_device vdev;
	struct mutex lock;
	struct pci_dev *pdev;
89

L
Linus Torvalds 已提交
90 91
	u32 iobase;
	u32 length;
92

L
Linus Torvalds 已提交
93 94 95 96
	u32 current_frequency;
	u8  mute;
};

97 98 99 100
static inline struct gemtek_pci *to_gemtek_pci(struct v4l2_device *v4l2_dev)
{
	return container_of(v4l2_dev, struct gemtek_pci, v4l2_dev);
}
L
Linus Torvalds 已提交
101

102
static inline u8 gemtek_pci_out(u16 value, u32 port)
L
Linus Torvalds 已提交
103
{
104
	outw(value, port);
L
Linus Torvalds 已提交
105 106 107 108

	return (u8)value;
}

109 110 111
#define _b0(v) (*((u8 *)&v))

static void __gemtek_pci_cmd(u16 value, u32 port, u8 *last_byte, int keep)
L
Linus Torvalds 已提交
112
{
113
	u8 byte = *last_byte;
L
Linus Torvalds 已提交
114

115 116
	if (!value) {
		if (!keep)
L
Linus Torvalds 已提交
117
			value = (u16)port;
118
		byte &= 0xfd;
L
Linus Torvalds 已提交
119 120 121
	} else
		byte |= 2;

122 123
	_b0(value) = byte;
	outw(value, port);
L
Linus Torvalds 已提交
124
	byte |= 1;
125 126
	_b0(value) = byte;
	outw(value, port);
L
Linus Torvalds 已提交
127
	byte &= 0xfe;
128 129
	_b0(value) = byte;
	outw(value, port);
130

L
Linus Torvalds 已提交
131 132 133
	*last_byte = byte;
}

134
static inline void gemtek_pci_nil(u32 port, u8 *last_byte)
L
Linus Torvalds 已提交
135
{
136
	__gemtek_pci_cmd(0x00, port, last_byte, false);
L
Linus Torvalds 已提交
137 138
}

139
static inline void gemtek_pci_cmd(u16 cmd, u32 port, u8 *last_byte)
L
Linus Torvalds 已提交
140
{
141
	__gemtek_pci_cmd(cmd, port, last_byte, true);
L
Linus Torvalds 已提交
142 143
}

144
static void gemtek_pci_setfrequency(struct gemtek_pci *card, unsigned long frequency)
L
Linus Torvalds 已提交
145
{
146 147 148
	int i;
	u32 value = frequency / 200 + 856;
	u16 mask = 0x8000;
L
Linus Torvalds 已提交
149 150 151
	u8 last_byte;
	u32 port = card->iobase;

152 153 154
	mutex_lock(&card->lock);
	card->current_frequency = frequency;
	last_byte = gemtek_pci_out(0x06, port);
L
Linus Torvalds 已提交
155 156 157

	i = 0;
	do {
158
		gemtek_pci_nil(port, &last_byte);
L
Linus Torvalds 已提交
159
		i++;
160
	} while (i < 9);
L
Linus Torvalds 已提交
161 162 163

	i = 0;
	do {
164
		gemtek_pci_cmd(value & mask, port, &last_byte);
L
Linus Torvalds 已提交
165 166
		mask >>= 1;
		i++;
167
	} while (i < 16);
L
Linus Torvalds 已提交
168

169 170
	outw(0x10, port);
	mutex_unlock(&card->lock);
L
Linus Torvalds 已提交
171 172 173
}


174
static void gemtek_pci_mute(struct gemtek_pci *card)
L
Linus Torvalds 已提交
175
{
176 177
	mutex_lock(&card->lock);
	outb(0x1f, card->iobase);
178
	card->mute = true;
179
	mutex_unlock(&card->lock);
L
Linus Torvalds 已提交
180 181
}

182
static void gemtek_pci_unmute(struct gemtek_pci *card)
L
Linus Torvalds 已提交
183
{
184 185
	if (card->mute) {
		gemtek_pci_setfrequency(card, card->current_frequency);
186
		card->mute = false;
L
Linus Torvalds 已提交
187 188 189
	}
}

190
static int gemtek_pci_getsignal(struct gemtek_pci *card)
L
Linus Torvalds 已提交
191
{
192 193 194 195 196 197
	int sig;

	mutex_lock(&card->lock);
	sig = (inb(card->iobase) & 0x08) ? 0 : 1;
	mutex_unlock(&card->lock);
	return sig;
L
Linus Torvalds 已提交
198 199
}

200 201 202
static int vidioc_querycap(struct file *file, void *priv,
					struct v4l2_capability *v)
{
203 204
	struct gemtek_pci *card = video_drvdata(file);

205 206
	strlcpy(v->driver, "radio-gemtek-pci", sizeof(v->driver));
	strlcpy(v->card, "GemTek PCI Radio", sizeof(v->card));
207
	snprintf(v->bus_info, sizeof(v->bus_info), "PCI:%s", pci_name(card->pdev));
208
	v->version = RADIO_VERSION;
209
	v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
210 211 212 213 214
	return 0;
}

static int vidioc_g_tuner(struct file *file, void *priv,
					struct v4l2_tuner *v)
L
Linus Torvalds 已提交
215
{
216
	struct gemtek_pci *card = video_drvdata(file);
L
Linus Torvalds 已提交
217

218 219 220
	if (v->index > 0)
		return -EINVAL;

221
	strlcpy(v->name, "FM", sizeof(v->name));
222 223 224 225 226 227 228 229 230
	v->type = V4L2_TUNER_RADIO;
	v->rangelow = GEMTEK_PCI_RANGE_LOW;
	v->rangehigh = GEMTEK_PCI_RANGE_HIGH;
	v->rxsubchans = V4L2_TUNER_SUB_MONO;
	v->capability = V4L2_TUNER_CAP_LOW;
	v->audmode = V4L2_TUNER_MODE_MONO;
	v->signal = 0xffff * gemtek_pci_getsignal(card);
	return 0;
}
L
Linus Torvalds 已提交
231

232 233 234
static int vidioc_s_tuner(struct file *file, void *priv,
					struct v4l2_tuner *v)
{
235
	return v->index ? -EINVAL : 0;
236
}
L
Linus Torvalds 已提交
237

238 239 240
static int vidioc_s_frequency(struct file *file, void *priv,
					struct v4l2_frequency *f)
{
241
	struct gemtek_pci *card = video_drvdata(file);
242

243 244
	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
		return -EINVAL;
245 246
	if (f->frequency < GEMTEK_PCI_RANGE_LOW ||
	    f->frequency > GEMTEK_PCI_RANGE_HIGH)
247 248 249 250 251
		return -EINVAL;
	gemtek_pci_setfrequency(card, f->frequency);
	card->mute = false;
	return 0;
}
252

253 254 255
static int vidioc_g_frequency(struct file *file, void *priv,
					struct v4l2_frequency *f)
{
256
	struct gemtek_pci *card = video_drvdata(file);
257

258 259
	if (f->tuner != 0)
		return -EINVAL;
260 261 262 263
	f->type = V4L2_TUNER_RADIO;
	f->frequency = card->current_frequency;
	return 0;
}
L
Linus Torvalds 已提交
264

265 266 267
static int vidioc_queryctrl(struct file *file, void *priv,
					struct v4l2_queryctrl *qc)
{
268 269 270 271 272
	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, 65535, 65535, 65535);
273 274 275 276 277 278 279
	}
	return -EINVAL;
}

static int vidioc_g_ctrl(struct file *file, void *priv,
					struct v4l2_control *ctrl)
{
280
	struct gemtek_pci *card = video_drvdata(file);
281

282 283 284 285 286 287 288 289 290 291 292 293 294
	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		ctrl->value = card->mute;
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
		if (card->mute)
			ctrl->value = 0;
		else
			ctrl->value = 65535;
		return 0;
	}
	return -EINVAL;
}
L
Linus Torvalds 已提交
295

296 297 298
static int vidioc_s_ctrl(struct file *file, void *priv,
					struct v4l2_control *ctrl)
{
299
	struct gemtek_pci *card = video_drvdata(file);
L
Linus Torvalds 已提交
300

301 302 303 304 305 306 307 308 309 310 311 312 313
	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		if (ctrl->value)
			gemtek_pci_mute(card);
		else
			gemtek_pci_unmute(card);
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
		if (ctrl->value)
			gemtek_pci_unmute(card);
		else
			gemtek_pci_mute(card);
		return 0;
L
Linus Torvalds 已提交
314
	}
315 316 317 318 319 320 321 322 323 324 325
	return -EINVAL;
}

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)
{
326 327 328 329 330 331 332 333 334
	return i ? -EINVAL : 0;
}

static int vidioc_g_audio(struct file *file, void *priv,
					struct v4l2_audio *a)
{
	a->index = 0;
	strlcpy(a->name, "Radio", sizeof(a->name));
	a->capability = V4L2_AUDCAP_STEREO;
335
	return 0;
L
Linus Torvalds 已提交
336 337
}

338 339
static int vidioc_s_audio(struct file *file, void *priv,
					struct v4l2_audio *a)
L
Linus Torvalds 已提交
340
{
341
	return a->index ? -EINVAL : 0;
L
Linus Torvalds 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
}

enum {
	GEMTEK_PR103
};

static char *card_names[] __devinitdata = {
	"GEMTEK_PR103"
};

static struct pci_device_id gemtek_pci_id[] =
{
	{ PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
	{ 0 }
};

359
MODULE_DEVICE_TABLE(pci, gemtek_pci_id);
L
Linus Torvalds 已提交
360

361
static const struct v4l2_file_operations gemtek_pci_fops = {
L
Linus Torvalds 已提交
362
	.owner		= THIS_MODULE,
363
	.ioctl		= video_ioctl2,
L
Linus Torvalds 已提交
364 365
};

366
static const struct v4l2_ioctl_ops gemtek_pci_ioctl_ops = {
367 368 369 370 371 372 373 374 375 376 377 378
	.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 已提交
379 380
};

381
static int __devinit gemtek_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
L
Linus Torvalds 已提交
382
{
383 384 385
	struct gemtek_pci *card;
	struct v4l2_device *v4l2_dev;
	int res;
L
Linus Torvalds 已提交
386

387 388 389
	card = kzalloc(sizeof(struct gemtek_pci), GFP_KERNEL);
	if (card == NULL) {
		dev_err(&pdev->dev, "out of memory\n");
L
Linus Torvalds 已提交
390 391 392
		return -ENOMEM;
	}

393 394 395
	v4l2_dev = &card->v4l2_dev;
	mutex_init(&card->lock);
	card->pdev = pdev;
396

397
	strlcpy(v4l2_dev->name, "gemtek_pci", sizeof(v4l2_dev->name));
L
Linus Torvalds 已提交
398

399 400 401 402 403
	res = v4l2_device_register(&pdev->dev, v4l2_dev);
	if (res < 0) {
		v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
		kfree(card);
		return res;
L
Linus Torvalds 已提交
404 405
	}

406 407
	if (pci_enable_device(pdev))
		goto err_pci;
408

409 410 411 412 413 414
	card->iobase = pci_resource_start(pdev, 0);
	card->length = pci_resource_len(pdev, 0);

	if (request_region(card->iobase, card->length, card_names[pci_id->driver_data]) == NULL) {
		v4l2_err(v4l2_dev, "i/o port already in use\n");
		goto err_pci;
L
Linus Torvalds 已提交
415 416
	}

417 418 419 420 421 422 423 424
	strlcpy(card->vdev.name, v4l2_dev->name, sizeof(card->vdev.name));
	card->vdev.v4l2_dev = v4l2_dev;
	card->vdev.fops = &gemtek_pci_fops;
	card->vdev.ioctl_ops = &gemtek_pci_ioctl_ops;
	card->vdev.release = video_device_release_empty;
	video_set_drvdata(&card->vdev, card);

	if (video_register_device(&card->vdev, VFL_TYPE_RADIO, nr_radio) < 0)
L
Linus Torvalds 已提交
425 426
		goto err_video;

427
	gemtek_pci_mute(card);
L
Linus Torvalds 已提交
428

429 430
	v4l2_info(v4l2_dev, "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
		pdev->revision, card->iobase, card->iobase + card->length - 1);
L
Linus Torvalds 已提交
431 432 433 434

	return 0;

err_video:
435
	release_region(card->iobase, card->length);
L
Linus Torvalds 已提交
436 437

err_pci:
438 439
	v4l2_device_unregister(v4l2_dev);
	kfree(card);
440
	return -ENODEV;
L
Linus Torvalds 已提交
441 442
}

443
static void __devexit gemtek_pci_remove(struct pci_dev *pdev)
L
Linus Torvalds 已提交
444
{
445 446
	struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
	struct gemtek_pci *card = to_gemtek_pci(v4l2_dev);
L
Linus Torvalds 已提交
447

448 449
	video_unregister_device(&card->vdev);
	v4l2_device_unregister(v4l2_dev);
L
Linus Torvalds 已提交
450

451
	release_region(card->iobase, card->length);
452

453 454
	if (mx)
		gemtek_pci_mute(card);
L
Linus Torvalds 已提交
455

456
	kfree(card);
L
Linus Torvalds 已提交
457 458
}

459
static struct pci_driver gemtek_pci_driver = {
L
Linus Torvalds 已提交
460 461 462 463 464 465
	.name		= "gemtek_pci",
	.id_table	= gemtek_pci_id,
	.probe		= gemtek_pci_probe,
	.remove		= __devexit_p(gemtek_pci_remove),
};

466
static int __init gemtek_pci_init(void)
L
Linus Torvalds 已提交
467
{
468
	return pci_register_driver(&gemtek_pci_driver);
L
Linus Torvalds 已提交
469 470
}

471
static void __exit gemtek_pci_exit(void)
L
Linus Torvalds 已提交
472
{
473
	pci_unregister_driver(&gemtek_pci_driver);
L
Linus Torvalds 已提交
474 475
}

476 477
module_init(gemtek_pci_init);
module_exit(gemtek_pci_exit);