blob: 0fcd2b09218de56667fc1d3a47eca7095d9d369c [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 */
109#define BU2614_NOPS 8 /* Number of supported operations */
110
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;
Pekka Seppanen47536472007-10-01 00:27:55 -0300117 unsigned long bu2614data[BU2614_NOPS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
Pekka Seppanen47536472007-10-01 00:27:55 -0300120enum {
121 BU2614_VOID,
122 BU2614_FREQ, /* D0..D15, Frequency data */
123 BU2614_PORT, /* P0..P2, Output port control data */
124 BU2614_FMES, /* CT, Frequency measurement beginning data */
125 BU2614_STDF, /* R0..R2, Standard frequency data */
126 BU2614_SWIN, /* S, Switch between FMIN / AMIN */
127 BU2614_SWAL, /* PS, Swallow counter division (AMIN only) */
128 BU2614_FMUN, /* GT, Frequency measurement time and unlock */
129 BU2614_TEST /* TS, Test data is input */
130};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Pekka Seppanen47536472007-10-01 00:27:55 -0300132struct bu2614_op {
133 int op; /* Operation */
134 int size; /* Data size */
135};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Pekka Seppanen47536472007-10-01 00:27:55 -0300137static struct gemtek_device gemtek_unit;
138
139static struct bu2614_op bu2614ops[] = {
140 {.op = BU2614_FREQ,
141 .size = 0x10},
142 {.op = BU2614_PORT,
143 .size = 0x03},
144 {.op = BU2614_VOID,
145 .size = 0x04},
146 {.op = BU2614_FMES,
147 .size = 0x01},
148 {.op = BU2614_STDF,
149 .size = 0x03},
150 {.op = BU2614_SWIN,
151 .size = 0x01},
152 {.op = BU2614_SWAL,
153 .size = 0x01},
154 {.op = BU2614_VOID,
155 .size = 0x01},
156 {.op = BU2614_FMUN,
157 .size = 0x01},
158 {.op = BU2614_TEST,
159 .size = 0x01}
160};
161
162static spinlock_t lock;
163
164/*
165 * Set data which will be sent to BU2614FS.
166 */
167static void gemtek_bu2614_set(struct gemtek_device *dev, int op,
168 unsigned long data)
169{
170 int i, q;
171
172 for (i = 0, q = 0; q < ARRAY_SIZE(dev->bu2614data); ++i) {
173 if (bu2614ops[i].op == op) {
174 dev->bu2614data[q] =
175 data & ((1 << bu2614ops[i].size) - 1);
176 return;
177 }
178
179 if (bu2614ops[i].op != BU2614_VOID)
180 ++q;
181 }
182}
183
184/*
185 * Transmit settings to BU2614FS over GemTek IC.
186 */
187static void gemtek_bu2614_transmit(struct gemtek_device *dev)
188{
189 int i, bit, q, mute;
190
191 spin_lock(&lock);
192
193 mute = dev->muted ? GEMTEK_MT : 0x00;
194
195 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
196 udelay(SHORT_DELAY);
197 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
198 udelay(LONG_DELAY);
199
200 for (i = 0, q = 0; q < ARRAY_SIZE(dev->bu2614data); ++i) {
201 for (bit = 0; bit < bu2614ops[i].size; ++bit) {
202 if (bu2614ops[i].op != BU2614_VOID &&
203 dev->bu2614data[q] & (1 << bit)) {
204 outb_p(mute | GEMTEK_CE | GEMTEK_DA, io);
205 udelay(SHORT_DELAY);
206 outb_p(mute | GEMTEK_CE | GEMTEK_DA |
207 GEMTEK_CK, io);
208 udelay(SHORT_DELAY);
209 } else {
210 outb_p(mute | GEMTEK_CE, io);
211 udelay(SHORT_DELAY);
212 outb_p(mute | GEMTEK_CE | GEMTEK_CK, io);
213 udelay(SHORT_DELAY);
214 }
215 }
216
217 if (bu2614ops[i].op != BU2614_VOID)
218 ++q;
219 }
220
221 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
222 udelay(SHORT_DELAY);
223 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
224 udelay(LONG_DELAY);
225
226 spin_unlock(&lock);
227}
228
229/*
Trent Piepho857e5942007-10-01 00:32:25 -0300230 * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
Pekka Seppanen47536472007-10-01 00:27:55 -0300231 */
Trent Piepho857e5942007-10-01 00:32:25 -0300232static unsigned long gemtek_convfreq(unsigned long freq)
Pekka Seppanen47536472007-10-01 00:27:55 -0300233{
Trent Piepho857e5942007-10-01 00:32:25 -0300234 return ((freq<<FSCALE) + IF_OFFSET + REF_FREQ/2) / REF_FREQ;
Pekka Seppanen47536472007-10-01 00:27:55 -0300235}
236
237/*
238 * Set FM-frequency.
239 */
240static void gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
241{
242
243 if (keepmuted && hardmute && dev->muted)
244 return;
245
246 if (freq < GEMTEK_LOWFREQ)
247 freq = GEMTEK_LOWFREQ;
248 else if (freq > GEMTEK_HIGHFREQ)
249 freq = GEMTEK_HIGHFREQ;
250
251 dev->lastfreq = freq;
252 dev->muted = 0;
253
254 gemtek_bu2614_set(dev, BU2614_PORT, 0);
255 gemtek_bu2614_set(dev, BU2614_FMES, 0);
256 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
257 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
258 gemtek_bu2614_set(dev, BU2614_FMUN, 1); /* GT bit set */
259 gemtek_bu2614_set(dev, BU2614_TEST, 0);
260
Pekka Seppanen47536472007-10-01 00:27:55 -0300261 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
Trent Piepho857e5942007-10-01 00:32:25 -0300262 gemtek_bu2614_set(dev, BU2614_FREQ, gemtek_convfreq(freq));
Pekka Seppanen47536472007-10-01 00:27:55 -0300263
264 gemtek_bu2614_transmit(dev);
265}
266
267/*
268 * Set mute flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
270static void gemtek_mute(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 = 1;
Pekka Seppanen47536472007-10-01 00:27:55 -0300274
275 if (hardmute) {
276 /* Turn off PLL, disable data output */
277 gemtek_bu2614_set(dev, BU2614_PORT, 0);
278 gemtek_bu2614_set(dev, BU2614_FMES, 0); /* CT bit off */
279 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
280 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
281 gemtek_bu2614_set(dev, BU2614_FMUN, 0); /* GT bit off */
282 gemtek_bu2614_set(dev, BU2614_TEST, 0);
283 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_PLL_OFF);
284 gemtek_bu2614_set(dev, BU2614_FREQ, 0);
285 gemtek_bu2614_transmit(dev);
286 } else {
287 spin_lock(&lock);
288
289 /* Read bus contents (CE, CK and DA). */
290 i = inb_p(io);
291 /* Write it back with mute flag set. */
292 outb_p((i >> 5) | GEMTEK_MT, io);
293 udelay(SHORT_DELAY);
294
295 spin_unlock(&lock);
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Pekka Seppanen47536472007-10-01 00:27:55 -0300299/*
300 * Unset mute flag.
301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static void gemtek_unmute(struct gemtek_device *dev)
303{
Pekka Seppanen47536472007-10-01 00:27:55 -0300304 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 dev->muted = 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300306
307 if (hardmute) {
308 /* Turn PLL back on. */
309 gemtek_setfreq(dev, dev->lastfreq);
310 } else {
311 spin_lock(&lock);
312
313 i = inb_p(io);
314 outb_p(i >> 5, io);
315 udelay(SHORT_DELAY);
316
317 spin_unlock(&lock);
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Pekka Seppanen47536472007-10-01 00:27:55 -0300321/*
322 * Get signal strength (= stereo status).
323 */
324static inline int gemtek_getsigstr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Pekka Seppanen47536472007-10-01 00:27:55 -0300326 return inb_p(io) & GEMTEK_NS ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Pekka Seppanen47536472007-10-01 00:27:55 -0300329/*
330 * Check if requested card acts like GemTek Radio card.
331 */
332static int gemtek_verify(int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Pekka Seppanen47536472007-10-01 00:27:55 -0300334 static int verified = -1;
335 int i, q;
336
337 if (verified == port)
338 return 1;
339
340 spin_lock(&lock);
341
342 q = inb_p(port); /* Read bus contents before probing. */
343 /* Try to turn on CE, CK and DA respectively and check if card responds
344 properly. */
345 for (i = 0; i < 3; ++i) {
346 outb_p(1 << i, port);
347 udelay(SHORT_DELAY);
348
349 if ((inb_p(port) & (~GEMTEK_NS)) != (0x17 | (1 << (i + 5)))) {
350 spin_unlock(&lock);
351 return 0;
352 }
353 }
354 outb_p(q >> 5, port); /* Write bus contents back. */
355 udelay(SHORT_DELAY);
356
357 spin_unlock(&lock);
358 verified = port;
359
360 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Pekka Seppanen47536472007-10-01 00:27:55 -0300363/*
364 * Automatic probing for card.
365 */
366static int gemtek_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Pekka Seppanen47536472007-10-01 00:27:55 -0300368 int ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int i;
370
Pekka Seppanen47536472007-10-01 00:27:55 -0300371 if (!probe) {
372 printk(KERN_INFO "Automatic device probing disabled.\n");
373 return -1;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Pekka Seppanen47536472007-10-01 00:27:55 -0300376 printk(KERN_INFO "Automatic device probing enabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Pekka Seppanen47536472007-10-01 00:27:55 -0300378 for (i = 0; i < ARRAY_SIZE(ioports); ++i) {
379 printk(KERN_INFO "Trying I/O port 0x%x...\n", ioports[i]);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300380
Pekka Seppanen47536472007-10-01 00:27:55 -0300381 if (!request_region(ioports[i], 1, "gemtek-probe")) {
382 printk(KERN_WARNING "I/O port 0x%x busy!\n",
383 ioports[i]);
384 continue;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Pekka Seppanen47536472007-10-01 00:27:55 -0300387 if (gemtek_verify(ioports[i])) {
388 printk(KERN_INFO "Card found from I/O port "
389 "0x%x!\n", ioports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Pekka Seppanen47536472007-10-01 00:27:55 -0300391 release_region(ioports[i], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Pekka Seppanen47536472007-10-01 00:27:55 -0300393 io = ioports[i];
394 return io;
395 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300396
Pekka Seppanen47536472007-10-01 00:27:55 -0300397 release_region(ioports[i], 1);
398 }
399
400 printk(KERN_ERR "Automatic probing failed!\n");
401
402 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Pekka Seppanen47536472007-10-01 00:27:55 -0300405/*
406 * Video 4 Linux stuff.
407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Pekka Seppanen47536472007-10-01 00:27:55 -0300409static struct v4l2_queryctrl radio_qctrl[] = {
410 {
411 .id = V4L2_CID_AUDIO_MUTE,
412 .name = "Mute",
413 .minimum = 0,
414 .maximum = 1,
415 .default_value = 1,
416 .type = V4L2_CTRL_TYPE_BOOLEAN,
417 }, {
418 .id = V4L2_CID_AUDIO_VOLUME,
419 .name = "Volume",
420 .minimum = 0,
421 .maximum = 65535,
422 .step = 65535,
423 .default_value = 0xff,
424 .type = V4L2_CTRL_TYPE_INTEGER,
425 }
426};
427
428static struct file_operations gemtek_fops = {
429 .owner = THIS_MODULE,
430 .open = video_exclusive_open,
431 .release = video_exclusive_release,
432 .ioctl = video_ioctl2,
433 .compat_ioctl = v4l_compat_ioctl32,
434 .llseek = no_llseek
435};
436
437static int vidioc_querycap(struct file *file, void *priv,
438 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300440 strlcpy(v->driver, "radio-gemtek", sizeof(v->driver));
441 strlcpy(v->card, "GemTek", sizeof(v->card));
442 sprintf(v->bus_info, "ISA");
443 v->version = RADIO_VERSION;
444 v->capabilities = V4L2_CAP_TUNER;
445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Pekka Seppanen47536472007-10-01 00:27:55 -0300448static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300450 if (v->index > 0)
451 return -EINVAL;
452
453 strcpy(v->name, "FM");
454 v->type = V4L2_TUNER_RADIO;
Pekka Seppanen47536472007-10-01 00:27:55 -0300455 v->rangelow = GEMTEK_LOWFREQ;
456 v->rangehigh = GEMTEK_HIGHFREQ;
457 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
458 v->signal = 0xffff * gemtek_getsigstr();
459 if (v->signal) {
460 v->audmode = V4L2_TUNER_MODE_STEREO;
461 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
462 } else {
463 v->audmode = V4L2_TUNER_MODE_MONO;
464 v->rxsubchans = V4L2_TUNER_SUB_MONO;
465 }
466
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300467 return 0;
468}
469
Pekka Seppanen47536472007-10-01 00:27:55 -0300470static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300471{
472 if (v->index > 0)
473 return -EINVAL;
474 return 0;
475}
476
477static int vidioc_s_frequency(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300478 struct v4l2_frequency *f)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300479{
480 struct video_device *dev = video_devdata(file);
481 struct gemtek_device *rt = dev->priv;
482
Pekka Seppanen47536472007-10-01 00:27:55 -0300483 gemtek_setfreq(rt, f->frequency);
484
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300485 return 0;
486}
487
488static int vidioc_g_frequency(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300489 struct v4l2_frequency *f)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300490{
491 struct video_device *dev = video_devdata(file);
492 struct gemtek_device *rt = dev->priv;
493
494 f->type = V4L2_TUNER_RADIO;
Pekka Seppanen47536472007-10-01 00:27:55 -0300495 f->frequency = rt->lastfreq;
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300496 return 0;
497}
498
499static int vidioc_queryctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300500 struct v4l2_queryctrl *qc)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300501{
502 int i;
503
Pekka Seppanen47536472007-10-01 00:27:55 -0300504 for (i = 0; i < ARRAY_SIZE(radio_qctrl); ++i) {
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300505 if (qc->id && qc->id == radio_qctrl[i].id) {
Pekka Seppanen47536472007-10-01 00:27:55 -0300506 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300507 return 0;
508 }
509 }
510 return -EINVAL;
511}
512
513static int vidioc_g_ctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300514 struct v4l2_control *ctrl)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300515{
516 struct video_device *dev = video_devdata(file);
517 struct gemtek_device *rt = dev->priv;
518
519 switch (ctrl->id) {
520 case V4L2_CID_AUDIO_MUTE:
521 ctrl->value = rt->muted;
522 return 0;
523 case V4L2_CID_AUDIO_VOLUME:
524 if (rt->muted)
525 ctrl->value = 0;
526 else
527 ctrl->value = 65535;
528 return 0;
529 }
530 return -EINVAL;
531}
532
533static int vidioc_s_ctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300534 struct v4l2_control *ctrl)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300535{
536 struct video_device *dev = video_devdata(file);
537 struct gemtek_device *rt = dev->priv;
538
539 switch (ctrl->id) {
540 case V4L2_CID_AUDIO_MUTE:
541 if (ctrl->value)
542 gemtek_mute(rt);
543 else
544 gemtek_unmute(rt);
545 return 0;
546 case V4L2_CID_AUDIO_VOLUME:
547 if (ctrl->value)
548 gemtek_unmute(rt);
549 else
550 gemtek_mute(rt);
551 return 0;
552 }
553 return -EINVAL;
554}
555
Pekka Seppanen47536472007-10-01 00:27:55 -0300556static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300557{
558 if (a->index > 1)
559 return -EINVAL;
560
561 strcpy(a->name, "Radio");
562 a->capability = V4L2_AUDCAP_STEREO;
563 return 0;
564}
565
566static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
567{
568 *i = 0;
569 return 0;
570}
571
572static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
573{
574 if (i != 0)
575 return -EINVAL;
576 return 0;
577}
578
Pekka Seppanen47536472007-10-01 00:27:55 -0300579static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300580{
581 if (a->index != 0)
582 return -EINVAL;
583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Pekka Seppanen47536472007-10-01 00:27:55 -0300586static struct video_device gemtek_radio = {
587 .owner = THIS_MODULE,
588 .name = "GemTek Radio card",
589 .type = VID_TYPE_TUNER,
590 .hardware = VID_HARDWARE_GEMTEK,
591 .fops = &gemtek_fops,
592 .vidioc_querycap = vidioc_querycap,
593 .vidioc_g_tuner = vidioc_g_tuner,
594 .vidioc_s_tuner = vidioc_s_tuner,
595 .vidioc_g_audio = vidioc_g_audio,
596 .vidioc_s_audio = vidioc_s_audio,
597 .vidioc_g_input = vidioc_g_input,
598 .vidioc_s_input = vidioc_s_input,
599 .vidioc_g_frequency = vidioc_g_frequency,
600 .vidioc_s_frequency = vidioc_s_frequency,
601 .vidioc_queryctrl = vidioc_queryctrl,
602 .vidioc_g_ctrl = vidioc_g_ctrl,
603 .vidioc_s_ctrl = vidioc_s_ctrl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604};
605
Pekka Seppanen47536472007-10-01 00:27:55 -0300606/*
607 * Initialization / cleanup related stuff.
608 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Pekka Seppanen47536472007-10-01 00:27:55 -0300610/*
611 * Initilize card.
612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613static int __init gemtek_init(void)
614{
Pekka Seppanen47536472007-10-01 00:27:55 -0300615 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Pekka Seppanen47536472007-10-01 00:27:55 -0300617 printk(KERN_INFO RADIO_BANNER "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 spin_lock_init(&lock);
620
Pekka Seppanen47536472007-10-01 00:27:55 -0300621 gemtek_probe();
622 if (io) {
623 if (!request_region(io, 1, "gemtek")) {
624 printk(KERN_ERR "I/O port 0x%x already in use.\n", io);
625 return -EBUSY;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Pekka Seppanen47536472007-10-01 00:27:55 -0300628 if (!gemtek_verify(io))
629 printk(KERN_WARNING "Card at I/O port 0x%x does not "
630 "respond properly, check your "
631 "configuration.\n", io);
632 else
633 printk(KERN_INFO "Using I/O port 0x%x.\n", io);
634 } else if (probe) {
635 printk(KERN_ERR "Automatic probing failed and no "
636 "fixed I/O port defined.\n");
637 return -ENODEV;
638 } else {
639 printk(KERN_ERR "Automatic probing disabled but no fixed "
640 "I/O port defined.");
641 return -EINVAL;
642 }
643
644 gemtek_radio.priv = &gemtek_unit;
645
646 if (video_register_device(&gemtek_radio, VFL_TYPE_RADIO,
647 radio_nr) == -1) {
648 release_region(io, 1);
649 return -EBUSY;
650 }
651
652 /* Set defaults */
653 gemtek_unit.lastfreq = GEMTEK_LOWFREQ;
654 for (i = 0; i < ARRAY_SIZE(gemtek_unit.bu2614data); ++i)
655 gemtek_unit.bu2614data[i] = 0;
656
657 if (initmute)
658 gemtek_mute(&gemtek_unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 return 0;
661}
662
Pekka Seppanen47536472007-10-01 00:27:55 -0300663/*
664 * Module cleanup
665 */
666static void __exit gemtek_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Pekka Seppanen47536472007-10-01 00:27:55 -0300668 if (shutdown) {
669 hardmute = 1; /* Turn off PLL */
670 gemtek_mute(&gemtek_unit);
671 } else {
672 printk(KERN_INFO "Module unloaded but card not muted!\n");
673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 video_unregister_device(&gemtek_radio);
Pekka Seppanen47536472007-10-01 00:27:55 -0300676 release_region(io, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679module_init(gemtek_init);
Pekka Seppanen47536472007-10-01 00:27:55 -0300680module_exit(gemtek_exit);