blob: 0c963db03614b4232e002f2b1e0e3ebd5da6feaa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* GemTek radio card driver for Linux (C) 1998 Jonas Munsin <jmunsin@iki.fi>
2 *
3 * GemTek hasn't released any specs on the card, so the protocol had to
4 * be reverse engineered with dosemu.
5 *
6 * Besides the protocol changes, this is mostly a copy of:
7 *
8 * RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
11 * Converted to new API by Alan Cox <Alan.Cox@linux.org>
12 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
13 *
14 * TODO: Allow for more than one of these foolish entities :-)
15 *
Mauro Carvalho Chehabd1c4ecd2006-08-08 09:10:01 -030016 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/module.h> /* Modules */
20#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070021#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h> /* udelay */
23#include <asm/io.h> /* outb, outb_p */
24#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehabd1c4ecd2006-08-08 09:10:01 -030025#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030026#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/spinlock.h>
28
Pekka Seppanen47536472007-10-01 00:27:55 -030029#include <linux/version.h> /* for KERNEL_VERSION MACRO */
30#define RADIO_VERSION KERNEL_VERSION(0,0,3)
31#define RADIO_BANNER "GemTek Radio card driver: v0.0.3"
Mauro Carvalho Chehabd1c4ecd2006-08-08 09:10:01 -030032
Pekka Seppanen47536472007-10-01 00:27:55 -030033/*
34 * Module info.
35 */
36
37MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <pexu@kapsi.fi>");
38MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
39MODULE_LICENSE("GPL");
40
41/*
42 * Module params.
43 */
Mauro Carvalho Chehabd1c4ecd2006-08-08 09:10:01 -030044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#ifndef CONFIG_RADIO_GEMTEK_PORT
46#define CONFIG_RADIO_GEMTEK_PORT -1
47#endif
Pekka Seppanen47536472007-10-01 00:27:55 -030048#ifndef CONFIG_RADIO_GEMTEK_PROBE
49#define CONFIG_RADIO_GEMTEK_PROBE 1
50#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Pekka Seppanen47536472007-10-01 00:27:55 -030052static int io = CONFIG_RADIO_GEMTEK_PORT;
53static int probe = CONFIG_RADIO_GEMTEK_PROBE;
54static int hardmute;
55static int shutdown = 1;
56static int keepmuted = 1;
57static int initmute = 1;
58static int radio_nr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Pekka Seppanen47536472007-10-01 00:27:55 -030060module_param(io, int, 0444);
61MODULE_PARM_DESC(io, "Force I/O port for the GemTek Radio card if automatic"
62 "probing is disabled or fails. The most common I/O ports are: 0x20c "
63 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
64 " work for the combined sound/radiocard).");
65
66module_param(probe, bool, 0444);
67MODULE_PARM_DESC(probe, "Enable automatic device probing. Note: only the most "
68 "common I/O ports used by the card are probed.");
69
70module_param(hardmute, bool, 0644);
71MODULE_PARM_DESC(hardmute, "Enable `hard muting' by shutting down PLL, may "
72 "reduce static noise.");
73
74module_param(shutdown, bool, 0644);
75MODULE_PARM_DESC(shutdown, "Enable shutting down PLL and muting line when "
76 "module is unloaded.");
77
78module_param(keepmuted, bool, 0644);
79MODULE_PARM_DESC(keepmuted, "Keep card muted even when frequency is changed.");
80
81module_param(initmute, bool, 0444);
82MODULE_PARM_DESC(initmute, "Mute card when module is loaded.");
83
84module_param(radio_nr, int, 0444);
85
86/*
87 * Functions for controlling the card.
88 */
89#define GEMTEK_LOWFREQ (87*16000)
90#define GEMTEK_HIGHFREQ (108*16000)
91
Trent Piepho857e5942007-10-01 00:32:25 -030092/*
93 * Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal
94 * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
95 */
96#define FSCALE 8
97#define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
98#define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
99
Pekka Seppanen47536472007-10-01 00:27:55 -0300100#define GEMTEK_CK 0x01 /* Clock signal */
101#define GEMTEK_DA 0x02 /* Serial data */
102#define GEMTEK_CE 0x04 /* Chip enable */
103#define GEMTEK_NS 0x08 /* No signal */
104#define GEMTEK_MT 0x10 /* Line mute */
105#define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */
106#define GEMTEK_PLL_OFF 0x07 /* PLL off */
107
108#define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */
Pekka Seppanen47536472007-10-01 00:27:55 -0300109
110#define SHORT_DELAY 5 /* usec */
111#define LONG_DELAY 75 /* usec */
112
113struct gemtek_device {
114 unsigned long lastfreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int muted;
Trent Piephob25be972007-10-01 00:38:30 -0300116 u32 bu2614data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
Trent Piephob25be972007-10-01 00:38:30 -0300119#define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */
120#define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */
121#define BU2614_VOID_BITS 4 /* unused */
122#define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */
123#define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */
124#define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */
125#define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/
126#define BU2614_VOID2_BITS 1 /* unused */
127#define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */
128#define BU2614_TEST_BITS 1 /* TS, Test data is input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Trent Piephob25be972007-10-01 00:38:30 -0300130#define BU2614_FREQ_SHIFT 0
131#define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
132#define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
133#define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
134#define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
135#define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
136#define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
137#define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
138#define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
139#define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
140
141#define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \
142 BU2614_##field##_SHIFT)
143#define BU2614_PORT_MASK MKMASK(PORT)
144#define BU2614_FREQ_MASK MKMASK(FREQ)
145#define BU2614_VOID_MASK MKMASK(VOID)
146#define BU2614_FMES_MASK MKMASK(FMES)
147#define BU2614_STDF_MASK MKMASK(STDF)
148#define BU2614_SWIN_MASK MKMASK(SWIN)
149#define BU2614_SWAL_MASK MKMASK(SWAL)
150#define BU2614_VOID2_MASK MKMASK(VOID2)
151#define BU2614_FMUN_MASK MKMASK(FMUN)
152#define BU2614_TEST_MASK MKMASK(TEST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Pekka Seppanen47536472007-10-01 00:27:55 -0300154static struct gemtek_device gemtek_unit;
155
Pekka Seppanen47536472007-10-01 00:27:55 -0300156static spinlock_t lock;
157
158/*
159 * Set data which will be sent to BU2614FS.
160 */
Trent Piephob25be972007-10-01 00:38:30 -0300161#define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
162 ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
Pekka Seppanen47536472007-10-01 00:27:55 -0300163
164/*
165 * Transmit settings to BU2614FS over GemTek IC.
166 */
167static void gemtek_bu2614_transmit(struct gemtek_device *dev)
168{
169 int i, bit, q, mute;
170
171 spin_lock(&lock);
172
173 mute = dev->muted ? GEMTEK_MT : 0x00;
174
175 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
176 udelay(SHORT_DELAY);
177 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
178 udelay(LONG_DELAY);
179
Trent Piephob25be972007-10-01 00:38:30 -0300180 for (i = 0, q = dev->bu2614data; i < 32; i++, q >>= 1) {
181 bit = (q & 1) ? GEMTEK_DA : 0;
182 outb_p(mute | GEMTEK_CE | bit, io);
183 udelay(SHORT_DELAY);
184 outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, io);
185 udelay(SHORT_DELAY);
Pekka Seppanen47536472007-10-01 00:27:55 -0300186 }
187
188 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
189 udelay(SHORT_DELAY);
190 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
191 udelay(LONG_DELAY);
192
193 spin_unlock(&lock);
194}
195
196/*
Trent Piepho857e5942007-10-01 00:32:25 -0300197 * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
Pekka Seppanen47536472007-10-01 00:27:55 -0300198 */
Trent Piepho857e5942007-10-01 00:32:25 -0300199static unsigned long gemtek_convfreq(unsigned long freq)
Pekka Seppanen47536472007-10-01 00:27:55 -0300200{
Trent Piepho857e5942007-10-01 00:32:25 -0300201 return ((freq<<FSCALE) + IF_OFFSET + REF_FREQ/2) / REF_FREQ;
Pekka Seppanen47536472007-10-01 00:27:55 -0300202}
203
204/*
205 * Set FM-frequency.
206 */
207static void gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
208{
209
210 if (keepmuted && hardmute && dev->muted)
211 return;
212
213 if (freq < GEMTEK_LOWFREQ)
214 freq = GEMTEK_LOWFREQ;
215 else if (freq > GEMTEK_HIGHFREQ)
216 freq = GEMTEK_HIGHFREQ;
217
218 dev->lastfreq = freq;
219 dev->muted = 0;
220
221 gemtek_bu2614_set(dev, BU2614_PORT, 0);
222 gemtek_bu2614_set(dev, BU2614_FMES, 0);
223 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
224 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
225 gemtek_bu2614_set(dev, BU2614_FMUN, 1); /* GT bit set */
226 gemtek_bu2614_set(dev, BU2614_TEST, 0);
227
Pekka Seppanen47536472007-10-01 00:27:55 -0300228 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
Trent Piepho857e5942007-10-01 00:32:25 -0300229 gemtek_bu2614_set(dev, BU2614_FREQ, gemtek_convfreq(freq));
Pekka Seppanen47536472007-10-01 00:27:55 -0300230
231 gemtek_bu2614_transmit(dev);
232}
233
234/*
235 * Set mute flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
237static void gemtek_mute(struct gemtek_device *dev)
238{
Pekka Seppanen47536472007-10-01 00:27:55 -0300239 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 dev->muted = 1;
Pekka Seppanen47536472007-10-01 00:27:55 -0300241
242 if (hardmute) {
243 /* Turn off PLL, disable data output */
244 gemtek_bu2614_set(dev, BU2614_PORT, 0);
245 gemtek_bu2614_set(dev, BU2614_FMES, 0); /* CT bit off */
246 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
247 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
248 gemtek_bu2614_set(dev, BU2614_FMUN, 0); /* GT bit off */
249 gemtek_bu2614_set(dev, BU2614_TEST, 0);
250 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_PLL_OFF);
251 gemtek_bu2614_set(dev, BU2614_FREQ, 0);
252 gemtek_bu2614_transmit(dev);
253 } else {
254 spin_lock(&lock);
255
256 /* Read bus contents (CE, CK and DA). */
257 i = inb_p(io);
258 /* Write it back with mute flag set. */
259 outb_p((i >> 5) | GEMTEK_MT, io);
260 udelay(SHORT_DELAY);
261
262 spin_unlock(&lock);
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Pekka Seppanen47536472007-10-01 00:27:55 -0300266/*
267 * Unset mute flag.
268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static void gemtek_unmute(struct gemtek_device *dev)
270{
Pekka Seppanen47536472007-10-01 00:27:55 -0300271 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 dev->muted = 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300273
274 if (hardmute) {
275 /* Turn PLL back on. */
276 gemtek_setfreq(dev, dev->lastfreq);
277 } else {
278 spin_lock(&lock);
279
280 i = inb_p(io);
281 outb_p(i >> 5, io);
282 udelay(SHORT_DELAY);
283
284 spin_unlock(&lock);
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Pekka Seppanen47536472007-10-01 00:27:55 -0300288/*
289 * Get signal strength (= stereo status).
290 */
291static inline int gemtek_getsigstr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Pekka Seppanen47536472007-10-01 00:27:55 -0300293 return inb_p(io) & GEMTEK_NS ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Pekka Seppanen47536472007-10-01 00:27:55 -0300296/*
297 * Check if requested card acts like GemTek Radio card.
298 */
299static int gemtek_verify(int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Pekka Seppanen47536472007-10-01 00:27:55 -0300301 static int verified = -1;
302 int i, q;
303
304 if (verified == port)
305 return 1;
306
307 spin_lock(&lock);
308
309 q = inb_p(port); /* Read bus contents before probing. */
310 /* Try to turn on CE, CK and DA respectively and check if card responds
311 properly. */
312 for (i = 0; i < 3; ++i) {
313 outb_p(1 << i, port);
314 udelay(SHORT_DELAY);
315
316 if ((inb_p(port) & (~GEMTEK_NS)) != (0x17 | (1 << (i + 5)))) {
317 spin_unlock(&lock);
318 return 0;
319 }
320 }
321 outb_p(q >> 5, port); /* Write bus contents back. */
322 udelay(SHORT_DELAY);
323
324 spin_unlock(&lock);
325 verified = port;
326
327 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Pekka Seppanen47536472007-10-01 00:27:55 -0300330/*
331 * Automatic probing for card.
332 */
333static int gemtek_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Pekka Seppanen47536472007-10-01 00:27:55 -0300335 int ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int i;
337
Pekka Seppanen47536472007-10-01 00:27:55 -0300338 if (!probe) {
339 printk(KERN_INFO "Automatic device probing disabled.\n");
340 return -1;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Pekka Seppanen47536472007-10-01 00:27:55 -0300343 printk(KERN_INFO "Automatic device probing enabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Pekka Seppanen47536472007-10-01 00:27:55 -0300345 for (i = 0; i < ARRAY_SIZE(ioports); ++i) {
346 printk(KERN_INFO "Trying I/O port 0x%x...\n", ioports[i]);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300347
Pekka Seppanen47536472007-10-01 00:27:55 -0300348 if (!request_region(ioports[i], 1, "gemtek-probe")) {
349 printk(KERN_WARNING "I/O port 0x%x busy!\n",
350 ioports[i]);
351 continue;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Pekka Seppanen47536472007-10-01 00:27:55 -0300354 if (gemtek_verify(ioports[i])) {
355 printk(KERN_INFO "Card found from I/O port "
356 "0x%x!\n", ioports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Pekka Seppanen47536472007-10-01 00:27:55 -0300358 release_region(ioports[i], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Pekka Seppanen47536472007-10-01 00:27:55 -0300360 io = ioports[i];
361 return io;
362 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300363
Pekka Seppanen47536472007-10-01 00:27:55 -0300364 release_region(ioports[i], 1);
365 }
366
367 printk(KERN_ERR "Automatic probing failed!\n");
368
369 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Pekka Seppanen47536472007-10-01 00:27:55 -0300372/*
373 * Video 4 Linux stuff.
374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Pekka Seppanen47536472007-10-01 00:27:55 -0300376static struct v4l2_queryctrl radio_qctrl[] = {
377 {
378 .id = V4L2_CID_AUDIO_MUTE,
379 .name = "Mute",
380 .minimum = 0,
381 .maximum = 1,
382 .default_value = 1,
383 .type = V4L2_CTRL_TYPE_BOOLEAN,
384 }, {
385 .id = V4L2_CID_AUDIO_VOLUME,
386 .name = "Volume",
387 .minimum = 0,
388 .maximum = 65535,
389 .step = 65535,
390 .default_value = 0xff,
391 .type = V4L2_CTRL_TYPE_INTEGER,
392 }
393};
394
395static struct file_operations gemtek_fops = {
396 .owner = THIS_MODULE,
397 .open = video_exclusive_open,
398 .release = video_exclusive_release,
399 .ioctl = video_ioctl2,
400 .compat_ioctl = v4l_compat_ioctl32,
401 .llseek = no_llseek
402};
403
404static int vidioc_querycap(struct file *file, void *priv,
405 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300407 strlcpy(v->driver, "radio-gemtek", sizeof(v->driver));
408 strlcpy(v->card, "GemTek", sizeof(v->card));
409 sprintf(v->bus_info, "ISA");
410 v->version = RADIO_VERSION;
411 v->capabilities = V4L2_CAP_TUNER;
412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Pekka Seppanen47536472007-10-01 00:27:55 -0300415static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300417 if (v->index > 0)
418 return -EINVAL;
419
420 strcpy(v->name, "FM");
421 v->type = V4L2_TUNER_RADIO;
Pekka Seppanen47536472007-10-01 00:27:55 -0300422 v->rangelow = GEMTEK_LOWFREQ;
423 v->rangehigh = GEMTEK_HIGHFREQ;
424 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
425 v->signal = 0xffff * gemtek_getsigstr();
426 if (v->signal) {
427 v->audmode = V4L2_TUNER_MODE_STEREO;
428 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
429 } else {
430 v->audmode = V4L2_TUNER_MODE_MONO;
431 v->rxsubchans = V4L2_TUNER_SUB_MONO;
432 }
433
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300434 return 0;
435}
436
Pekka Seppanen47536472007-10-01 00:27:55 -0300437static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300438{
439 if (v->index > 0)
440 return -EINVAL;
441 return 0;
442}
443
444static int vidioc_s_frequency(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300445 struct v4l2_frequency *f)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300446{
447 struct video_device *dev = video_devdata(file);
448 struct gemtek_device *rt = dev->priv;
449
Pekka Seppanen47536472007-10-01 00:27:55 -0300450 gemtek_setfreq(rt, f->frequency);
451
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300452 return 0;
453}
454
455static int vidioc_g_frequency(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300456 struct v4l2_frequency *f)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300457{
458 struct video_device *dev = video_devdata(file);
459 struct gemtek_device *rt = dev->priv;
460
461 f->type = V4L2_TUNER_RADIO;
Pekka Seppanen47536472007-10-01 00:27:55 -0300462 f->frequency = rt->lastfreq;
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300463 return 0;
464}
465
466static int vidioc_queryctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300467 struct v4l2_queryctrl *qc)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300468{
469 int i;
470
Pekka Seppanen47536472007-10-01 00:27:55 -0300471 for (i = 0; i < ARRAY_SIZE(radio_qctrl); ++i) {
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300472 if (qc->id && qc->id == radio_qctrl[i].id) {
Pekka Seppanen47536472007-10-01 00:27:55 -0300473 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300474 return 0;
475 }
476 }
477 return -EINVAL;
478}
479
480static int vidioc_g_ctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300481 struct v4l2_control *ctrl)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300482{
483 struct video_device *dev = video_devdata(file);
484 struct gemtek_device *rt = dev->priv;
485
486 switch (ctrl->id) {
487 case V4L2_CID_AUDIO_MUTE:
488 ctrl->value = rt->muted;
489 return 0;
490 case V4L2_CID_AUDIO_VOLUME:
491 if (rt->muted)
492 ctrl->value = 0;
493 else
494 ctrl->value = 65535;
495 return 0;
496 }
497 return -EINVAL;
498}
499
500static int vidioc_s_ctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300501 struct v4l2_control *ctrl)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300502{
503 struct video_device *dev = video_devdata(file);
504 struct gemtek_device *rt = dev->priv;
505
506 switch (ctrl->id) {
507 case V4L2_CID_AUDIO_MUTE:
508 if (ctrl->value)
509 gemtek_mute(rt);
510 else
511 gemtek_unmute(rt);
512 return 0;
513 case V4L2_CID_AUDIO_VOLUME:
514 if (ctrl->value)
515 gemtek_unmute(rt);
516 else
517 gemtek_mute(rt);
518 return 0;
519 }
520 return -EINVAL;
521}
522
Pekka Seppanen47536472007-10-01 00:27:55 -0300523static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300524{
525 if (a->index > 1)
526 return -EINVAL;
527
528 strcpy(a->name, "Radio");
529 a->capability = V4L2_AUDCAP_STEREO;
530 return 0;
531}
532
533static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
534{
535 *i = 0;
536 return 0;
537}
538
539static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
540{
541 if (i != 0)
542 return -EINVAL;
543 return 0;
544}
545
Pekka Seppanen47536472007-10-01 00:27:55 -0300546static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300547{
548 if (a->index != 0)
549 return -EINVAL;
550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Pekka Seppanen47536472007-10-01 00:27:55 -0300553static struct video_device gemtek_radio = {
554 .owner = THIS_MODULE,
555 .name = "GemTek Radio card",
556 .type = VID_TYPE_TUNER,
557 .hardware = VID_HARDWARE_GEMTEK,
558 .fops = &gemtek_fops,
559 .vidioc_querycap = vidioc_querycap,
560 .vidioc_g_tuner = vidioc_g_tuner,
561 .vidioc_s_tuner = vidioc_s_tuner,
562 .vidioc_g_audio = vidioc_g_audio,
563 .vidioc_s_audio = vidioc_s_audio,
564 .vidioc_g_input = vidioc_g_input,
565 .vidioc_s_input = vidioc_s_input,
566 .vidioc_g_frequency = vidioc_g_frequency,
567 .vidioc_s_frequency = vidioc_s_frequency,
568 .vidioc_queryctrl = vidioc_queryctrl,
569 .vidioc_g_ctrl = vidioc_g_ctrl,
570 .vidioc_s_ctrl = vidioc_s_ctrl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571};
572
Pekka Seppanen47536472007-10-01 00:27:55 -0300573/*
574 * Initialization / cleanup related stuff.
575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Pekka Seppanen47536472007-10-01 00:27:55 -0300577/*
578 * Initilize card.
579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static int __init gemtek_init(void)
581{
Pekka Seppanen47536472007-10-01 00:27:55 -0300582 printk(KERN_INFO RADIO_BANNER "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 spin_lock_init(&lock);
585
Pekka Seppanen47536472007-10-01 00:27:55 -0300586 gemtek_probe();
587 if (io) {
588 if (!request_region(io, 1, "gemtek")) {
589 printk(KERN_ERR "I/O port 0x%x already in use.\n", io);
590 return -EBUSY;
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Pekka Seppanen47536472007-10-01 00:27:55 -0300593 if (!gemtek_verify(io))
594 printk(KERN_WARNING "Card at I/O port 0x%x does not "
595 "respond properly, check your "
596 "configuration.\n", io);
597 else
598 printk(KERN_INFO "Using I/O port 0x%x.\n", io);
599 } else if (probe) {
600 printk(KERN_ERR "Automatic probing failed and no "
601 "fixed I/O port defined.\n");
602 return -ENODEV;
603 } else {
604 printk(KERN_ERR "Automatic probing disabled but no fixed "
605 "I/O port defined.");
606 return -EINVAL;
607 }
608
609 gemtek_radio.priv = &gemtek_unit;
610
611 if (video_register_device(&gemtek_radio, VFL_TYPE_RADIO,
612 radio_nr) == -1) {
613 release_region(io, 1);
614 return -EBUSY;
615 }
616
617 /* Set defaults */
618 gemtek_unit.lastfreq = GEMTEK_LOWFREQ;
Trent Piephob25be972007-10-01 00:38:30 -0300619 gemtek_unit.bu2614data = 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300620
621 if (initmute)
622 gemtek_mute(&gemtek_unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 return 0;
625}
626
Pekka Seppanen47536472007-10-01 00:27:55 -0300627/*
628 * Module cleanup
629 */
630static void __exit gemtek_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Pekka Seppanen47536472007-10-01 00:27:55 -0300632 if (shutdown) {
633 hardmute = 1; /* Turn off PLL */
634 gemtek_mute(&gemtek_unit);
635 } else {
636 printk(KERN_INFO "Module unloaded but card not muted!\n");
637 }
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 video_unregister_device(&gemtek_radio);
Pekka Seppanen47536472007-10-01 00:27:55 -0300640 release_region(io, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643module_init(gemtek_init);
Pekka Seppanen47536472007-10-01 00:27:55 -0300644module_exit(gemtek_exit);