blob: 4740bacc2f88e43a59768f308223b9d2c3c3f9f1 [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 */
Hans Verkuil35ea11f2008-07-20 08:12:02 -030026#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030027#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/spinlock.h>
29
Pekka Seppanen47536472007-10-01 00:27:55 -030030#include <linux/version.h> /* for KERNEL_VERSION MACRO */
31#define RADIO_VERSION KERNEL_VERSION(0,0,3)
32#define RADIO_BANNER "GemTek Radio card driver: v0.0.3"
Mauro Carvalho Chehabd1c4ecd2006-08-08 09:10:01 -030033
Pekka Seppanen47536472007-10-01 00:27:55 -030034/*
35 * Module info.
36 */
37
38MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <pexu@kapsi.fi>");
39MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
40MODULE_LICENSE("GPL");
41
42/*
43 * Module params.
44 */
Mauro Carvalho Chehabd1c4ecd2006-08-08 09:10:01 -030045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#ifndef CONFIG_RADIO_GEMTEK_PORT
47#define CONFIG_RADIO_GEMTEK_PORT -1
48#endif
Pekka Seppanen47536472007-10-01 00:27:55 -030049#ifndef CONFIG_RADIO_GEMTEK_PROBE
50#define CONFIG_RADIO_GEMTEK_PROBE 1
51#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Pekka Seppanen47536472007-10-01 00:27:55 -030053static int io = CONFIG_RADIO_GEMTEK_PORT;
54static int probe = CONFIG_RADIO_GEMTEK_PROBE;
55static int hardmute;
56static int shutdown = 1;
57static int keepmuted = 1;
58static int initmute = 1;
59static int radio_nr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Pekka Seppanen47536472007-10-01 00:27:55 -030061module_param(io, int, 0444);
Joe Perches13d97012007-11-19 22:48:16 -030062MODULE_PARM_DESC(io, "Force I/O port for the GemTek Radio card if automatic "
Pekka Seppanen47536472007-10-01 00:27:55 -030063 "probing is disabled or fails. The most common I/O ports are: 0x20c "
64 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
Joe Perches13d97012007-11-19 22:48:16 -030065 "work for the combined sound/radiocard).");
Pekka Seppanen47536472007-10-01 00:27:55 -030066
67module_param(probe, bool, 0444);
68MODULE_PARM_DESC(probe, "Enable automatic device probing. Note: only the most "
69 "common I/O ports used by the card are probed.");
70
71module_param(hardmute, bool, 0644);
72MODULE_PARM_DESC(hardmute, "Enable `hard muting' by shutting down PLL, may "
73 "reduce static noise.");
74
75module_param(shutdown, bool, 0644);
76MODULE_PARM_DESC(shutdown, "Enable shutting down PLL and muting line when "
77 "module is unloaded.");
78
79module_param(keepmuted, bool, 0644);
80MODULE_PARM_DESC(keepmuted, "Keep card muted even when frequency is changed.");
81
82module_param(initmute, bool, 0444);
83MODULE_PARM_DESC(initmute, "Mute card when module is loaded.");
84
85module_param(radio_nr, int, 0444);
86
87/*
88 * Functions for controlling the card.
89 */
90#define GEMTEK_LOWFREQ (87*16000)
91#define GEMTEK_HIGHFREQ (108*16000)
92
Trent Piepho857e5942007-10-01 00:32:25 -030093/*
94 * Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal
95 * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
96 */
97#define FSCALE 8
98#define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
99#define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
100
Pekka Seppanen47536472007-10-01 00:27:55 -0300101#define GEMTEK_CK 0x01 /* Clock signal */
102#define GEMTEK_DA 0x02 /* Serial data */
103#define GEMTEK_CE 0x04 /* Chip enable */
104#define GEMTEK_NS 0x08 /* No signal */
105#define GEMTEK_MT 0x10 /* Line mute */
106#define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */
107#define GEMTEK_PLL_OFF 0x07 /* PLL off */
108
109#define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */
Pekka Seppanen47536472007-10-01 00:27:55 -0300110
111#define SHORT_DELAY 5 /* usec */
112#define LONG_DELAY 75 /* usec */
113
114struct gemtek_device {
115 unsigned long lastfreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int muted;
Trent Piephob25be972007-10-01 00:38:30 -0300117 u32 bu2614data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
Trent Piephob25be972007-10-01 00:38:30 -0300120#define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */
121#define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */
122#define BU2614_VOID_BITS 4 /* unused */
123#define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */
124#define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */
125#define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */
126#define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/
127#define BU2614_VOID2_BITS 1 /* unused */
128#define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */
129#define BU2614_TEST_BITS 1 /* TS, Test data is input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Trent Piephob25be972007-10-01 00:38:30 -0300131#define BU2614_FREQ_SHIFT 0
132#define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
133#define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
134#define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
135#define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
136#define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
137#define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
138#define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
139#define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
140#define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
141
142#define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \
143 BU2614_##field##_SHIFT)
144#define BU2614_PORT_MASK MKMASK(PORT)
145#define BU2614_FREQ_MASK MKMASK(FREQ)
146#define BU2614_VOID_MASK MKMASK(VOID)
147#define BU2614_FMES_MASK MKMASK(FMES)
148#define BU2614_STDF_MASK MKMASK(STDF)
149#define BU2614_SWIN_MASK MKMASK(SWIN)
150#define BU2614_SWAL_MASK MKMASK(SWAL)
151#define BU2614_VOID2_MASK MKMASK(VOID2)
152#define BU2614_FMUN_MASK MKMASK(FMUN)
153#define BU2614_TEST_MASK MKMASK(TEST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Pekka Seppanen47536472007-10-01 00:27:55 -0300155static struct gemtek_device gemtek_unit;
156
Pekka Seppanen47536472007-10-01 00:27:55 -0300157static spinlock_t lock;
158
159/*
160 * Set data which will be sent to BU2614FS.
161 */
Trent Piephob25be972007-10-01 00:38:30 -0300162#define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
163 ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
Pekka Seppanen47536472007-10-01 00:27:55 -0300164
165/*
166 * Transmit settings to BU2614FS over GemTek IC.
167 */
168static void gemtek_bu2614_transmit(struct gemtek_device *dev)
169{
170 int i, bit, q, mute;
171
172 spin_lock(&lock);
173
174 mute = dev->muted ? GEMTEK_MT : 0x00;
175
176 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
177 udelay(SHORT_DELAY);
178 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
179 udelay(LONG_DELAY);
180
Trent Piephob25be972007-10-01 00:38:30 -0300181 for (i = 0, q = dev->bu2614data; i < 32; i++, q >>= 1) {
182 bit = (q & 1) ? GEMTEK_DA : 0;
183 outb_p(mute | GEMTEK_CE | bit, io);
184 udelay(SHORT_DELAY);
185 outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, io);
186 udelay(SHORT_DELAY);
Pekka Seppanen47536472007-10-01 00:27:55 -0300187 }
188
189 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
190 udelay(SHORT_DELAY);
191 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
192 udelay(LONG_DELAY);
193
194 spin_unlock(&lock);
195}
196
197/*
Trent Piepho857e5942007-10-01 00:32:25 -0300198 * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
Pekka Seppanen47536472007-10-01 00:27:55 -0300199 */
Trent Piepho857e5942007-10-01 00:32:25 -0300200static unsigned long gemtek_convfreq(unsigned long freq)
Pekka Seppanen47536472007-10-01 00:27:55 -0300201{
Trent Piepho857e5942007-10-01 00:32:25 -0300202 return ((freq<<FSCALE) + IF_OFFSET + REF_FREQ/2) / REF_FREQ;
Pekka Seppanen47536472007-10-01 00:27:55 -0300203}
204
205/*
206 * Set FM-frequency.
207 */
208static void gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
209{
210
211 if (keepmuted && hardmute && dev->muted)
212 return;
213
214 if (freq < GEMTEK_LOWFREQ)
215 freq = GEMTEK_LOWFREQ;
216 else if (freq > GEMTEK_HIGHFREQ)
217 freq = GEMTEK_HIGHFREQ;
218
219 dev->lastfreq = freq;
220 dev->muted = 0;
221
222 gemtek_bu2614_set(dev, BU2614_PORT, 0);
223 gemtek_bu2614_set(dev, BU2614_FMES, 0);
224 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
225 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
226 gemtek_bu2614_set(dev, BU2614_FMUN, 1); /* GT bit set */
227 gemtek_bu2614_set(dev, BU2614_TEST, 0);
228
Pekka Seppanen47536472007-10-01 00:27:55 -0300229 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
Trent Piepho857e5942007-10-01 00:32:25 -0300230 gemtek_bu2614_set(dev, BU2614_FREQ, gemtek_convfreq(freq));
Pekka Seppanen47536472007-10-01 00:27:55 -0300231
232 gemtek_bu2614_transmit(dev);
233}
234
235/*
236 * Set mute flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
238static void gemtek_mute(struct gemtek_device *dev)
239{
Pekka Seppanen47536472007-10-01 00:27:55 -0300240 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 dev->muted = 1;
Pekka Seppanen47536472007-10-01 00:27:55 -0300242
243 if (hardmute) {
244 /* Turn off PLL, disable data output */
245 gemtek_bu2614_set(dev, BU2614_PORT, 0);
246 gemtek_bu2614_set(dev, BU2614_FMES, 0); /* CT bit off */
247 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
248 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
249 gemtek_bu2614_set(dev, BU2614_FMUN, 0); /* GT bit off */
250 gemtek_bu2614_set(dev, BU2614_TEST, 0);
251 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_PLL_OFF);
252 gemtek_bu2614_set(dev, BU2614_FREQ, 0);
253 gemtek_bu2614_transmit(dev);
254 } else {
255 spin_lock(&lock);
256
257 /* Read bus contents (CE, CK and DA). */
258 i = inb_p(io);
259 /* Write it back with mute flag set. */
260 outb_p((i >> 5) | GEMTEK_MT, io);
261 udelay(SHORT_DELAY);
262
263 spin_unlock(&lock);
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Pekka Seppanen47536472007-10-01 00:27:55 -0300267/*
268 * Unset mute flag.
269 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static void gemtek_unmute(struct gemtek_device *dev)
271{
Pekka Seppanen47536472007-10-01 00:27:55 -0300272 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dev->muted = 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300274
275 if (hardmute) {
276 /* Turn PLL back on. */
277 gemtek_setfreq(dev, dev->lastfreq);
278 } else {
279 spin_lock(&lock);
280
281 i = inb_p(io);
282 outb_p(i >> 5, io);
283 udelay(SHORT_DELAY);
284
285 spin_unlock(&lock);
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Pekka Seppanen47536472007-10-01 00:27:55 -0300289/*
290 * Get signal strength (= stereo status).
291 */
292static inline int gemtek_getsigstr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Pekka Seppanen47536472007-10-01 00:27:55 -0300294 return inb_p(io) & GEMTEK_NS ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Pekka Seppanen47536472007-10-01 00:27:55 -0300297/*
298 * Check if requested card acts like GemTek Radio card.
299 */
300static int gemtek_verify(int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Pekka Seppanen47536472007-10-01 00:27:55 -0300302 static int verified = -1;
303 int i, q;
304
305 if (verified == port)
306 return 1;
307
308 spin_lock(&lock);
309
310 q = inb_p(port); /* Read bus contents before probing. */
311 /* Try to turn on CE, CK and DA respectively and check if card responds
312 properly. */
313 for (i = 0; i < 3; ++i) {
314 outb_p(1 << i, port);
315 udelay(SHORT_DELAY);
316
317 if ((inb_p(port) & (~GEMTEK_NS)) != (0x17 | (1 << (i + 5)))) {
318 spin_unlock(&lock);
319 return 0;
320 }
321 }
322 outb_p(q >> 5, port); /* Write bus contents back. */
323 udelay(SHORT_DELAY);
324
325 spin_unlock(&lock);
326 verified = port;
327
328 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Pekka Seppanen47536472007-10-01 00:27:55 -0300331/*
332 * Automatic probing for card.
333 */
334static int gemtek_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Pekka Seppanen47536472007-10-01 00:27:55 -0300336 int ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int i;
338
Pekka Seppanen47536472007-10-01 00:27:55 -0300339 if (!probe) {
340 printk(KERN_INFO "Automatic device probing disabled.\n");
341 return -1;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Pekka Seppanen47536472007-10-01 00:27:55 -0300344 printk(KERN_INFO "Automatic device probing enabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Pekka Seppanen47536472007-10-01 00:27:55 -0300346 for (i = 0; i < ARRAY_SIZE(ioports); ++i) {
347 printk(KERN_INFO "Trying I/O port 0x%x...\n", ioports[i]);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300348
Pekka Seppanen47536472007-10-01 00:27:55 -0300349 if (!request_region(ioports[i], 1, "gemtek-probe")) {
350 printk(KERN_WARNING "I/O port 0x%x busy!\n",
351 ioports[i]);
352 continue;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Pekka Seppanen47536472007-10-01 00:27:55 -0300355 if (gemtek_verify(ioports[i])) {
356 printk(KERN_INFO "Card found from I/O port "
357 "0x%x!\n", ioports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Pekka Seppanen47536472007-10-01 00:27:55 -0300359 release_region(ioports[i], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Pekka Seppanen47536472007-10-01 00:27:55 -0300361 io = ioports[i];
362 return io;
363 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300364
Pekka Seppanen47536472007-10-01 00:27:55 -0300365 release_region(ioports[i], 1);
366 }
367
368 printk(KERN_ERR "Automatic probing failed!\n");
369
370 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Pekka Seppanen47536472007-10-01 00:27:55 -0300373/*
374 * Video 4 Linux stuff.
375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Pekka Seppanen47536472007-10-01 00:27:55 -0300377static struct v4l2_queryctrl radio_qctrl[] = {
378 {
379 .id = V4L2_CID_AUDIO_MUTE,
380 .name = "Mute",
381 .minimum = 0,
382 .maximum = 1,
383 .default_value = 1,
384 .type = V4L2_CTRL_TYPE_BOOLEAN,
385 }, {
386 .id = V4L2_CID_AUDIO_VOLUME,
387 .name = "Volume",
388 .minimum = 0,
389 .maximum = 65535,
390 .step = 65535,
391 .default_value = 0xff,
392 .type = V4L2_CTRL_TYPE_INTEGER,
393 }
394};
395
Douglas Schilling Landgrafc7ac61c2007-10-28 23:41:19 -0300396static const struct file_operations gemtek_fops = {
Pekka Seppanen47536472007-10-01 00:27:55 -0300397 .owner = THIS_MODULE,
398 .open = video_exclusive_open,
399 .release = video_exclusive_release,
400 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300401#ifdef CONFIG_COMPAT
Pekka Seppanen47536472007-10-01 00:27:55 -0300402 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300403#endif
Pekka Seppanen47536472007-10-01 00:27:55 -0300404 .llseek = no_llseek
405};
406
407static int vidioc_querycap(struct file *file, void *priv,
408 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300410 strlcpy(v->driver, "radio-gemtek", sizeof(v->driver));
411 strlcpy(v->card, "GemTek", sizeof(v->card));
412 sprintf(v->bus_info, "ISA");
413 v->version = RADIO_VERSION;
414 v->capabilities = V4L2_CAP_TUNER;
415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Pekka Seppanen47536472007-10-01 00:27:55 -0300418static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300420 if (v->index > 0)
421 return -EINVAL;
422
423 strcpy(v->name, "FM");
424 v->type = V4L2_TUNER_RADIO;
Pekka Seppanen47536472007-10-01 00:27:55 -0300425 v->rangelow = GEMTEK_LOWFREQ;
426 v->rangehigh = GEMTEK_HIGHFREQ;
427 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
428 v->signal = 0xffff * gemtek_getsigstr();
429 if (v->signal) {
430 v->audmode = V4L2_TUNER_MODE_STEREO;
431 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
432 } else {
433 v->audmode = V4L2_TUNER_MODE_MONO;
434 v->rxsubchans = V4L2_TUNER_SUB_MONO;
435 }
436
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300437 return 0;
438}
439
Pekka Seppanen47536472007-10-01 00:27:55 -0300440static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300441{
442 if (v->index > 0)
443 return -EINVAL;
444 return 0;
445}
446
447static int vidioc_s_frequency(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300448 struct v4l2_frequency *f)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300449{
450 struct video_device *dev = video_devdata(file);
451 struct gemtek_device *rt = dev->priv;
452
Pekka Seppanen47536472007-10-01 00:27:55 -0300453 gemtek_setfreq(rt, f->frequency);
454
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300455 return 0;
456}
457
458static int vidioc_g_frequency(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300459 struct v4l2_frequency *f)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300460{
461 struct video_device *dev = video_devdata(file);
462 struct gemtek_device *rt = dev->priv;
463
464 f->type = V4L2_TUNER_RADIO;
Pekka Seppanen47536472007-10-01 00:27:55 -0300465 f->frequency = rt->lastfreq;
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300466 return 0;
467}
468
469static int vidioc_queryctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300470 struct v4l2_queryctrl *qc)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300471{
472 int i;
473
Pekka Seppanen47536472007-10-01 00:27:55 -0300474 for (i = 0; i < ARRAY_SIZE(radio_qctrl); ++i) {
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300475 if (qc->id && qc->id == radio_qctrl[i].id) {
Pekka Seppanen47536472007-10-01 00:27:55 -0300476 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300477 return 0;
478 }
479 }
480 return -EINVAL;
481}
482
483static int vidioc_g_ctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300484 struct v4l2_control *ctrl)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300485{
486 struct video_device *dev = video_devdata(file);
487 struct gemtek_device *rt = dev->priv;
488
489 switch (ctrl->id) {
490 case V4L2_CID_AUDIO_MUTE:
491 ctrl->value = rt->muted;
492 return 0;
493 case V4L2_CID_AUDIO_VOLUME:
494 if (rt->muted)
495 ctrl->value = 0;
496 else
497 ctrl->value = 65535;
498 return 0;
499 }
500 return -EINVAL;
501}
502
503static int vidioc_s_ctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300504 struct v4l2_control *ctrl)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300505{
506 struct video_device *dev = video_devdata(file);
507 struct gemtek_device *rt = dev->priv;
508
509 switch (ctrl->id) {
510 case V4L2_CID_AUDIO_MUTE:
511 if (ctrl->value)
512 gemtek_mute(rt);
513 else
514 gemtek_unmute(rt);
515 return 0;
516 case V4L2_CID_AUDIO_VOLUME:
517 if (ctrl->value)
518 gemtek_unmute(rt);
519 else
520 gemtek_mute(rt);
521 return 0;
522 }
523 return -EINVAL;
524}
525
Pekka Seppanen47536472007-10-01 00:27:55 -0300526static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300527{
528 if (a->index > 1)
529 return -EINVAL;
530
531 strcpy(a->name, "Radio");
532 a->capability = V4L2_AUDCAP_STEREO;
533 return 0;
534}
535
536static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
537{
538 *i = 0;
539 return 0;
540}
541
542static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
543{
544 if (i != 0)
545 return -EINVAL;
546 return 0;
547}
548
Pekka Seppanen47536472007-10-01 00:27:55 -0300549static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300550{
551 if (a->index != 0)
552 return -EINVAL;
553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Pekka Seppanen47536472007-10-01 00:27:55 -0300556static struct video_device gemtek_radio = {
557 .owner = THIS_MODULE,
558 .name = "GemTek Radio card",
559 .type = VID_TYPE_TUNER,
Pekka Seppanen47536472007-10-01 00:27:55 -0300560 .fops = &gemtek_fops,
561 .vidioc_querycap = vidioc_querycap,
562 .vidioc_g_tuner = vidioc_g_tuner,
563 .vidioc_s_tuner = vidioc_s_tuner,
564 .vidioc_g_audio = vidioc_g_audio,
565 .vidioc_s_audio = vidioc_s_audio,
566 .vidioc_g_input = vidioc_g_input,
567 .vidioc_s_input = vidioc_s_input,
568 .vidioc_g_frequency = vidioc_g_frequency,
569 .vidioc_s_frequency = vidioc_s_frequency,
570 .vidioc_queryctrl = vidioc_queryctrl,
571 .vidioc_g_ctrl = vidioc_g_ctrl,
572 .vidioc_s_ctrl = vidioc_s_ctrl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573};
574
Pekka Seppanen47536472007-10-01 00:27:55 -0300575/*
576 * Initialization / cleanup related stuff.
577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Pekka Seppanen47536472007-10-01 00:27:55 -0300579/*
580 * Initilize card.
581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static int __init gemtek_init(void)
583{
Pekka Seppanen47536472007-10-01 00:27:55 -0300584 printk(KERN_INFO RADIO_BANNER "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 spin_lock_init(&lock);
587
Pekka Seppanen47536472007-10-01 00:27:55 -0300588 gemtek_probe();
589 if (io) {
590 if (!request_region(io, 1, "gemtek")) {
591 printk(KERN_ERR "I/O port 0x%x already in use.\n", io);
592 return -EBUSY;
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Pekka Seppanen47536472007-10-01 00:27:55 -0300595 if (!gemtek_verify(io))
596 printk(KERN_WARNING "Card at I/O port 0x%x does not "
597 "respond properly, check your "
598 "configuration.\n", io);
599 else
600 printk(KERN_INFO "Using I/O port 0x%x.\n", io);
601 } else if (probe) {
602 printk(KERN_ERR "Automatic probing failed and no "
603 "fixed I/O port defined.\n");
604 return -ENODEV;
605 } else {
606 printk(KERN_ERR "Automatic probing disabled but no fixed "
607 "I/O port defined.");
608 return -EINVAL;
609 }
610
611 gemtek_radio.priv = &gemtek_unit;
612
613 if (video_register_device(&gemtek_radio, VFL_TYPE_RADIO,
614 radio_nr) == -1) {
615 release_region(io, 1);
616 return -EBUSY;
617 }
618
619 /* Set defaults */
620 gemtek_unit.lastfreq = GEMTEK_LOWFREQ;
Trent Piephob25be972007-10-01 00:38:30 -0300621 gemtek_unit.bu2614data = 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300622
623 if (initmute)
624 gemtek_mute(&gemtek_unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 return 0;
627}
628
Pekka Seppanen47536472007-10-01 00:27:55 -0300629/*
630 * Module cleanup
631 */
632static void __exit gemtek_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Pekka Seppanen47536472007-10-01 00:27:55 -0300634 if (shutdown) {
635 hardmute = 1; /* Turn off PLL */
636 gemtek_mute(&gemtek_unit);
637 } else {
638 printk(KERN_INFO "Module unloaded but card not muted!\n");
639 }
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 video_unregister_device(&gemtek_radio);
Pekka Seppanen47536472007-10-01 00:27:55 -0300642 release_region(io, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645module_init(gemtek_init);
Pekka Seppanen47536472007-10-01 00:27:55 -0300646module_exit(gemtek_exit);