blob: e13118da307bb313eab757c9cead6046c59aa2ee [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
Alan Coxd9b01442008-10-27 15:13:47 -030011 * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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;
Hans Verkuil3ca685a2008-08-23 04:49:13 -030060static unsigned long in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Pekka Seppanen47536472007-10-01 00:27:55 -030062module_param(io, int, 0444);
Joe Perches13d97012007-11-19 22:48:16 -030063MODULE_PARM_DESC(io, "Force I/O port for the GemTek Radio card if automatic "
Pekka Seppanen47536472007-10-01 00:27:55 -030064 "probing is disabled or fails. The most common I/O ports are: 0x20c "
65 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
Joe Perches13d97012007-11-19 22:48:16 -030066 "work for the combined sound/radiocard).");
Pekka Seppanen47536472007-10-01 00:27:55 -030067
68module_param(probe, bool, 0444);
69MODULE_PARM_DESC(probe, "Enable automatic device probing. Note: only the most "
70 "common I/O ports used by the card are probed.");
71
72module_param(hardmute, bool, 0644);
73MODULE_PARM_DESC(hardmute, "Enable `hard muting' by shutting down PLL, may "
74 "reduce static noise.");
75
76module_param(shutdown, bool, 0644);
77MODULE_PARM_DESC(shutdown, "Enable shutting down PLL and muting line when "
78 "module is unloaded.");
79
80module_param(keepmuted, bool, 0644);
81MODULE_PARM_DESC(keepmuted, "Keep card muted even when frequency is changed.");
82
83module_param(initmute, bool, 0444);
84MODULE_PARM_DESC(initmute, "Mute card when module is loaded.");
85
86module_param(radio_nr, int, 0444);
87
88/*
89 * Functions for controlling the card.
90 */
91#define GEMTEK_LOWFREQ (87*16000)
92#define GEMTEK_HIGHFREQ (108*16000)
93
Trent Piepho857e5942007-10-01 00:32:25 -030094/*
95 * Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal
96 * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
97 */
98#define FSCALE 8
99#define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
100#define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
101
Pekka Seppanen47536472007-10-01 00:27:55 -0300102#define GEMTEK_CK 0x01 /* Clock signal */
103#define GEMTEK_DA 0x02 /* Serial data */
104#define GEMTEK_CE 0x04 /* Chip enable */
105#define GEMTEK_NS 0x08 /* No signal */
106#define GEMTEK_MT 0x10 /* Line mute */
107#define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */
108#define GEMTEK_PLL_OFF 0x07 /* PLL off */
109
110#define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */
Pekka Seppanen47536472007-10-01 00:27:55 -0300111
112#define SHORT_DELAY 5 /* usec */
113#define LONG_DELAY 75 /* usec */
114
115struct gemtek_device {
116 unsigned long lastfreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int muted;
Trent Piephob25be972007-10-01 00:38:30 -0300118 u32 bu2614data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Trent Piephob25be972007-10-01 00:38:30 -0300121#define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */
122#define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */
123#define BU2614_VOID_BITS 4 /* unused */
124#define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */
125#define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */
126#define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */
127#define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/
128#define BU2614_VOID2_BITS 1 /* unused */
129#define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */
130#define BU2614_TEST_BITS 1 /* TS, Test data is input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Trent Piephob25be972007-10-01 00:38:30 -0300132#define BU2614_FREQ_SHIFT 0
133#define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
134#define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
135#define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
136#define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
137#define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
138#define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
139#define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
140#define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
141#define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
142
143#define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \
144 BU2614_##field##_SHIFT)
145#define BU2614_PORT_MASK MKMASK(PORT)
146#define BU2614_FREQ_MASK MKMASK(FREQ)
147#define BU2614_VOID_MASK MKMASK(VOID)
148#define BU2614_FMES_MASK MKMASK(FMES)
149#define BU2614_STDF_MASK MKMASK(STDF)
150#define BU2614_SWIN_MASK MKMASK(SWIN)
151#define BU2614_SWAL_MASK MKMASK(SWAL)
152#define BU2614_VOID2_MASK MKMASK(VOID2)
153#define BU2614_FMUN_MASK MKMASK(FMUN)
154#define BU2614_TEST_MASK MKMASK(TEST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Pekka Seppanen47536472007-10-01 00:27:55 -0300156static struct gemtek_device gemtek_unit;
157
Pekka Seppanen47536472007-10-01 00:27:55 -0300158static spinlock_t lock;
159
160/*
161 * Set data which will be sent to BU2614FS.
162 */
Trent Piephob25be972007-10-01 00:38:30 -0300163#define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
164 ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
Pekka Seppanen47536472007-10-01 00:27:55 -0300165
166/*
167 * Transmit settings to BU2614FS over GemTek IC.
168 */
169static void gemtek_bu2614_transmit(struct gemtek_device *dev)
170{
171 int i, bit, q, mute;
172
173 spin_lock(&lock);
174
175 mute = dev->muted ? GEMTEK_MT : 0x00;
176
177 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
178 udelay(SHORT_DELAY);
179 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
180 udelay(LONG_DELAY);
181
Trent Piephob25be972007-10-01 00:38:30 -0300182 for (i = 0, q = dev->bu2614data; i < 32; i++, q >>= 1) {
183 bit = (q & 1) ? GEMTEK_DA : 0;
184 outb_p(mute | GEMTEK_CE | bit, io);
185 udelay(SHORT_DELAY);
186 outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, io);
187 udelay(SHORT_DELAY);
Pekka Seppanen47536472007-10-01 00:27:55 -0300188 }
189
190 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
191 udelay(SHORT_DELAY);
192 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
193 udelay(LONG_DELAY);
194
195 spin_unlock(&lock);
196}
197
198/*
Trent Piepho857e5942007-10-01 00:32:25 -0300199 * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
Pekka Seppanen47536472007-10-01 00:27:55 -0300200 */
Trent Piepho857e5942007-10-01 00:32:25 -0300201static unsigned long gemtek_convfreq(unsigned long freq)
Pekka Seppanen47536472007-10-01 00:27:55 -0300202{
Trent Piepho857e5942007-10-01 00:32:25 -0300203 return ((freq<<FSCALE) + IF_OFFSET + REF_FREQ/2) / REF_FREQ;
Pekka Seppanen47536472007-10-01 00:27:55 -0300204}
205
206/*
207 * Set FM-frequency.
208 */
209static void gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
210{
211
212 if (keepmuted && hardmute && dev->muted)
213 return;
214
215 if (freq < GEMTEK_LOWFREQ)
216 freq = GEMTEK_LOWFREQ;
217 else if (freq > GEMTEK_HIGHFREQ)
218 freq = GEMTEK_HIGHFREQ;
219
220 dev->lastfreq = freq;
221 dev->muted = 0;
222
223 gemtek_bu2614_set(dev, BU2614_PORT, 0);
224 gemtek_bu2614_set(dev, BU2614_FMES, 0);
225 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
226 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
227 gemtek_bu2614_set(dev, BU2614_FMUN, 1); /* GT bit set */
228 gemtek_bu2614_set(dev, BU2614_TEST, 0);
229
Pekka Seppanen47536472007-10-01 00:27:55 -0300230 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
Trent Piepho857e5942007-10-01 00:32:25 -0300231 gemtek_bu2614_set(dev, BU2614_FREQ, gemtek_convfreq(freq));
Pekka Seppanen47536472007-10-01 00:27:55 -0300232
233 gemtek_bu2614_transmit(dev);
234}
235
236/*
237 * Set mute flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239static void gemtek_mute(struct gemtek_device *dev)
240{
Pekka Seppanen47536472007-10-01 00:27:55 -0300241 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 dev->muted = 1;
Pekka Seppanen47536472007-10-01 00:27:55 -0300243
244 if (hardmute) {
245 /* Turn off PLL, disable data output */
246 gemtek_bu2614_set(dev, BU2614_PORT, 0);
247 gemtek_bu2614_set(dev, BU2614_FMES, 0); /* CT bit off */
248 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
249 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
250 gemtek_bu2614_set(dev, BU2614_FMUN, 0); /* GT bit off */
251 gemtek_bu2614_set(dev, BU2614_TEST, 0);
252 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_PLL_OFF);
253 gemtek_bu2614_set(dev, BU2614_FREQ, 0);
254 gemtek_bu2614_transmit(dev);
255 } else {
256 spin_lock(&lock);
257
258 /* Read bus contents (CE, CK and DA). */
259 i = inb_p(io);
260 /* Write it back with mute flag set. */
261 outb_p((i >> 5) | GEMTEK_MT, io);
262 udelay(SHORT_DELAY);
263
264 spin_unlock(&lock);
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Pekka Seppanen47536472007-10-01 00:27:55 -0300268/*
269 * Unset mute flag.
270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static void gemtek_unmute(struct gemtek_device *dev)
272{
Pekka Seppanen47536472007-10-01 00:27:55 -0300273 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 dev->muted = 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300275
276 if (hardmute) {
277 /* Turn PLL back on. */
278 gemtek_setfreq(dev, dev->lastfreq);
279 } else {
280 spin_lock(&lock);
281
282 i = inb_p(io);
283 outb_p(i >> 5, io);
284 udelay(SHORT_DELAY);
285
286 spin_unlock(&lock);
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Pekka Seppanen47536472007-10-01 00:27:55 -0300290/*
291 * Get signal strength (= stereo status).
292 */
293static inline int gemtek_getsigstr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Pekka Seppanen47536472007-10-01 00:27:55 -0300295 return inb_p(io) & GEMTEK_NS ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Pekka Seppanen47536472007-10-01 00:27:55 -0300298/*
299 * Check if requested card acts like GemTek Radio card.
300 */
301static int gemtek_verify(int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Pekka Seppanen47536472007-10-01 00:27:55 -0300303 static int verified = -1;
304 int i, q;
305
306 if (verified == port)
307 return 1;
308
309 spin_lock(&lock);
310
311 q = inb_p(port); /* Read bus contents before probing. */
312 /* Try to turn on CE, CK and DA respectively and check if card responds
313 properly. */
314 for (i = 0; i < 3; ++i) {
315 outb_p(1 << i, port);
316 udelay(SHORT_DELAY);
317
318 if ((inb_p(port) & (~GEMTEK_NS)) != (0x17 | (1 << (i + 5)))) {
319 spin_unlock(&lock);
320 return 0;
321 }
322 }
323 outb_p(q >> 5, port); /* Write bus contents back. */
324 udelay(SHORT_DELAY);
325
326 spin_unlock(&lock);
327 verified = port;
328
329 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Pekka Seppanen47536472007-10-01 00:27:55 -0300332/*
333 * Automatic probing for card.
334 */
335static int gemtek_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Pekka Seppanen47536472007-10-01 00:27:55 -0300337 int ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int i;
339
Pekka Seppanen47536472007-10-01 00:27:55 -0300340 if (!probe) {
341 printk(KERN_INFO "Automatic device probing disabled.\n");
342 return -1;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Pekka Seppanen47536472007-10-01 00:27:55 -0300345 printk(KERN_INFO "Automatic device probing enabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Pekka Seppanen47536472007-10-01 00:27:55 -0300347 for (i = 0; i < ARRAY_SIZE(ioports); ++i) {
348 printk(KERN_INFO "Trying I/O port 0x%x...\n", ioports[i]);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300349
Pekka Seppanen47536472007-10-01 00:27:55 -0300350 if (!request_region(ioports[i], 1, "gemtek-probe")) {
351 printk(KERN_WARNING "I/O port 0x%x busy!\n",
352 ioports[i]);
353 continue;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Pekka Seppanen47536472007-10-01 00:27:55 -0300356 if (gemtek_verify(ioports[i])) {
357 printk(KERN_INFO "Card found from I/O port "
358 "0x%x!\n", ioports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Pekka Seppanen47536472007-10-01 00:27:55 -0300360 release_region(ioports[i], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Pekka Seppanen47536472007-10-01 00:27:55 -0300362 io = ioports[i];
363 return io;
364 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300365
Pekka Seppanen47536472007-10-01 00:27:55 -0300366 release_region(ioports[i], 1);
367 }
368
369 printk(KERN_ERR "Automatic probing failed!\n");
370
371 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Pekka Seppanen47536472007-10-01 00:27:55 -0300374/*
375 * Video 4 Linux stuff.
376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Pekka Seppanen47536472007-10-01 00:27:55 -0300378static struct v4l2_queryctrl radio_qctrl[] = {
379 {
380 .id = V4L2_CID_AUDIO_MUTE,
381 .name = "Mute",
382 .minimum = 0,
383 .maximum = 1,
384 .default_value = 1,
385 .type = V4L2_CTRL_TYPE_BOOLEAN,
386 }, {
387 .id = V4L2_CID_AUDIO_VOLUME,
388 .name = "Volume",
389 .minimum = 0,
390 .maximum = 65535,
391 .step = 65535,
392 .default_value = 0xff,
393 .type = V4L2_CTRL_TYPE_INTEGER,
394 }
395};
396
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300397static int gemtek_exclusive_open(struct inode *inode, struct file *file)
398{
399 return test_and_set_bit(0, &in_use) ? -EBUSY : 0;
400}
401
402static int gemtek_exclusive_release(struct inode *inode, struct file *file)
403{
404 clear_bit(0, &in_use);
405 return 0;
406}
407
Douglas Schilling Landgrafc7ac61c2007-10-28 23:41:19 -0300408static const struct file_operations gemtek_fops = {
Pekka Seppanen47536472007-10-01 00:27:55 -0300409 .owner = THIS_MODULE,
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300410 .open = gemtek_exclusive_open,
411 .release = gemtek_exclusive_release,
Pekka Seppanen47536472007-10-01 00:27:55 -0300412 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300413#ifdef CONFIG_COMPAT
Pekka Seppanen47536472007-10-01 00:27:55 -0300414 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300415#endif
Pekka Seppanen47536472007-10-01 00:27:55 -0300416 .llseek = no_llseek
417};
418
419static int vidioc_querycap(struct file *file, void *priv,
420 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300422 strlcpy(v->driver, "radio-gemtek", sizeof(v->driver));
423 strlcpy(v->card, "GemTek", sizeof(v->card));
424 sprintf(v->bus_info, "ISA");
425 v->version = RADIO_VERSION;
426 v->capabilities = V4L2_CAP_TUNER;
427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Pekka Seppanen47536472007-10-01 00:27:55 -0300430static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300432 if (v->index > 0)
433 return -EINVAL;
434
435 strcpy(v->name, "FM");
436 v->type = V4L2_TUNER_RADIO;
Pekka Seppanen47536472007-10-01 00:27:55 -0300437 v->rangelow = GEMTEK_LOWFREQ;
438 v->rangehigh = GEMTEK_HIGHFREQ;
439 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
440 v->signal = 0xffff * gemtek_getsigstr();
441 if (v->signal) {
442 v->audmode = V4L2_TUNER_MODE_STEREO;
443 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
444 } else {
445 v->audmode = V4L2_TUNER_MODE_MONO;
446 v->rxsubchans = V4L2_TUNER_SUB_MONO;
447 }
448
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300449 return 0;
450}
451
Pekka Seppanen47536472007-10-01 00:27:55 -0300452static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300453{
454 if (v->index > 0)
455 return -EINVAL;
456 return 0;
457}
458
459static int vidioc_s_frequency(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300460 struct v4l2_frequency *f)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300461{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300462 struct gemtek_device *rt = video_drvdata(file);
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300463
Pekka Seppanen47536472007-10-01 00:27:55 -0300464 gemtek_setfreq(rt, f->frequency);
465
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300466 return 0;
467}
468
469static int vidioc_g_frequency(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300470 struct v4l2_frequency *f)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300471{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300472 struct gemtek_device *rt = video_drvdata(file);
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300473
474 f->type = V4L2_TUNER_RADIO;
Pekka Seppanen47536472007-10-01 00:27:55 -0300475 f->frequency = rt->lastfreq;
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300476 return 0;
477}
478
479static int vidioc_queryctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300480 struct v4l2_queryctrl *qc)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300481{
482 int i;
483
Pekka Seppanen47536472007-10-01 00:27:55 -0300484 for (i = 0; i < ARRAY_SIZE(radio_qctrl); ++i) {
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300485 if (qc->id && qc->id == radio_qctrl[i].id) {
Pekka Seppanen47536472007-10-01 00:27:55 -0300486 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300487 return 0;
488 }
489 }
490 return -EINVAL;
491}
492
493static int vidioc_g_ctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300494 struct v4l2_control *ctrl)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300495{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300496 struct gemtek_device *rt = video_drvdata(file);
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300497
498 switch (ctrl->id) {
499 case V4L2_CID_AUDIO_MUTE:
500 ctrl->value = rt->muted;
501 return 0;
502 case V4L2_CID_AUDIO_VOLUME:
503 if (rt->muted)
504 ctrl->value = 0;
505 else
506 ctrl->value = 65535;
507 return 0;
508 }
509 return -EINVAL;
510}
511
512static int vidioc_s_ctrl(struct file *file, void *priv,
Pekka Seppanen47536472007-10-01 00:27:55 -0300513 struct v4l2_control *ctrl)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300514{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300515 struct gemtek_device *rt = video_drvdata(file);
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300516
517 switch (ctrl->id) {
518 case V4L2_CID_AUDIO_MUTE:
519 if (ctrl->value)
520 gemtek_mute(rt);
521 else
522 gemtek_unmute(rt);
523 return 0;
524 case V4L2_CID_AUDIO_VOLUME:
525 if (ctrl->value)
526 gemtek_unmute(rt);
527 else
528 gemtek_mute(rt);
529 return 0;
530 }
531 return -EINVAL;
532}
533
Pekka Seppanen47536472007-10-01 00:27:55 -0300534static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300535{
536 if (a->index > 1)
537 return -EINVAL;
538
539 strcpy(a->name, "Radio");
540 a->capability = V4L2_AUDCAP_STEREO;
541 return 0;
542}
543
544static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
545{
546 *i = 0;
547 return 0;
548}
549
550static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
551{
552 if (i != 0)
553 return -EINVAL;
554 return 0;
555}
556
Pekka Seppanen47536472007-10-01 00:27:55 -0300557static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
Douglas Landgrafe9bb9c62007-04-25 00:15:46 -0300558{
559 if (a->index != 0)
560 return -EINVAL;
561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Hans Verkuila3998102008-07-21 02:57:38 -0300564static const struct v4l2_ioctl_ops gemtek_ioctl_ops = {
Pekka Seppanen47536472007-10-01 00:27:55 -0300565 .vidioc_querycap = vidioc_querycap,
566 .vidioc_g_tuner = vidioc_g_tuner,
567 .vidioc_s_tuner = vidioc_s_tuner,
568 .vidioc_g_audio = vidioc_g_audio,
569 .vidioc_s_audio = vidioc_s_audio,
570 .vidioc_g_input = vidioc_g_input,
571 .vidioc_s_input = vidioc_s_input,
572 .vidioc_g_frequency = vidioc_g_frequency,
573 .vidioc_s_frequency = vidioc_s_frequency,
574 .vidioc_queryctrl = vidioc_queryctrl,
575 .vidioc_g_ctrl = vidioc_g_ctrl,
576 .vidioc_s_ctrl = vidioc_s_ctrl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577};
578
Hans Verkuila3998102008-07-21 02:57:38 -0300579static struct video_device gemtek_radio = {
Hans Verkuilaa5e90a2008-08-23 06:23:55 -0300580 .name = "GemTek Radio card",
581 .fops = &gemtek_fops,
582 .ioctl_ops = &gemtek_ioctl_ops,
583 .release = video_device_release_empty,
Hans Verkuila3998102008-07-21 02:57:38 -0300584};
585
Pekka Seppanen47536472007-10-01 00:27:55 -0300586/*
587 * Initialization / cleanup related stuff.
588 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Pekka Seppanen47536472007-10-01 00:27:55 -0300590/*
591 * Initilize card.
592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static int __init gemtek_init(void)
594{
Pekka Seppanen47536472007-10-01 00:27:55 -0300595 printk(KERN_INFO RADIO_BANNER "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 spin_lock_init(&lock);
598
Pekka Seppanen47536472007-10-01 00:27:55 -0300599 gemtek_probe();
600 if (io) {
601 if (!request_region(io, 1, "gemtek")) {
602 printk(KERN_ERR "I/O port 0x%x already in use.\n", io);
603 return -EBUSY;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Pekka Seppanen47536472007-10-01 00:27:55 -0300606 if (!gemtek_verify(io))
607 printk(KERN_WARNING "Card at I/O port 0x%x does not "
608 "respond properly, check your "
609 "configuration.\n", io);
610 else
611 printk(KERN_INFO "Using I/O port 0x%x.\n", io);
612 } else if (probe) {
613 printk(KERN_ERR "Automatic probing failed and no "
614 "fixed I/O port defined.\n");
615 return -ENODEV;
616 } else {
617 printk(KERN_ERR "Automatic probing disabled but no fixed "
618 "I/O port defined.");
619 return -EINVAL;
620 }
621
Hans Verkuil601e9442008-08-23 07:24:07 -0300622 video_set_drvdata(&gemtek_radio, &gemtek_unit);
Pekka Seppanen47536472007-10-01 00:27:55 -0300623
Hans Verkuilcba99ae2008-09-03 17:11:58 -0300624 if (video_register_device(&gemtek_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
Pekka Seppanen47536472007-10-01 00:27:55 -0300625 release_region(io, 1);
626 return -EBUSY;
627 }
628
629 /* Set defaults */
630 gemtek_unit.lastfreq = GEMTEK_LOWFREQ;
Trent Piephob25be972007-10-01 00:38:30 -0300631 gemtek_unit.bu2614data = 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300632
633 if (initmute)
634 gemtek_mute(&gemtek_unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 return 0;
637}
638
Pekka Seppanen47536472007-10-01 00:27:55 -0300639/*
640 * Module cleanup
641 */
642static void __exit gemtek_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Pekka Seppanen47536472007-10-01 00:27:55 -0300644 if (shutdown) {
645 hardmute = 1; /* Turn off PLL */
646 gemtek_mute(&gemtek_unit);
647 } else {
648 printk(KERN_INFO "Module unloaded but card not muted!\n");
649 }
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 video_unregister_device(&gemtek_radio);
Pekka Seppanen47536472007-10-01 00:27:55 -0300652 release_region(io, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655module_init(gemtek_init);
Pekka Seppanen47536472007-10-01 00:27:55 -0300656module_exit(gemtek_exit);