blob: 19fb7fec4135131ab27f1a1dead68b153fd62343 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* SF16FMR2 radio driver for Linux radio support
2 * heavily based on fmi driver...
3 * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
4 *
5 * Notes on the hardware
6 *
7 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
8 * No volume control - only mute/unmute - you have to use line volume
9 *
10 * For read stereo/mono you must wait 0.1 sec after set frequency and
11 * card unmuted so I set frequency on unmute
12 * Signal handling seem to work only on autoscanning (not implemented)
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030013 *
14 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#include <linux/module.h> /* Modules */
18#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070019#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h> /* udelay */
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030021#include <linux/videodev2.h> /* kernel radio structs */
Ingo Molnar3593cab2006-02-07 06:49:14 -020022#include <linux/mutex.h>
Mauro Carvalho Chehab1ddf5bc2006-08-08 09:10:06 -030023#include <linux/version.h> /* for KERNEL_VERSION MACRO */
Hans Verkuilc32a9d72009-03-06 13:53:26 -030024#include <linux/io.h> /* outb, outb_p */
25#include <linux/uaccess.h> /* copy to/from user */
26#include <media/v4l2-device.h>
27#include <media/v4l2-ioctl.h>
28
29MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
30MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
31MODULE_LICENSE("GPL");
32
33static int io = 0x384;
34static int radio_nr = -1;
35
36module_param(io, int, 0);
37MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
38module_param(radio_nr, int, 0);
39
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030040#define RADIO_VERSION KERNEL_VERSION(0,0,2)
41
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -030042#define AUD_VOL_INDEX 1
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#undef DEBUG
45//#define DEBUG 1
46
47#ifdef DEBUG
48# define debug_print(s) printk s
49#else
50# define debug_print(s)
51#endif
52
53/* this should be static vars for module size */
Hans Verkuilc32a9d72009-03-06 13:53:26 -030054struct fmr2
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Hans Verkuilc32a9d72009-03-06 13:53:26 -030056 struct v4l2_device v4l2_dev;
57 struct video_device vdev;
58 struct mutex lock;
59 int io;
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -030060 int curvol; /* 0-15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int mute;
62 int stereo; /* card is producing stereo audio */
63 unsigned long curfreq; /* freq in kHz */
64 int card_type;
Hans Verkuilc32a9d72009-03-06 13:53:26 -030065 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Hans Verkuilc32a9d72009-03-06 13:53:26 -030068static struct fmr2 fmr2_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* hw precision is 12.5 kHz
Adrian Bunka58a4142006-01-10 00:08:17 +010071 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * other bits will be truncated
73 */
Hans Verkuilc32a9d72009-03-06 13:53:26 -030074#define RSF16_ENCODE(x) ((x) / 200 + 856)
75#define RSF16_MINFREQ (87 * 16000)
76#define RSF16_MAXFREQ (108 * 16000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Hans Verkuilc32a9d72009-03-06 13:53:26 -030078static inline void wait(int n, int io)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Hans Verkuilc32a9d72009-03-06 13:53:26 -030080 for (; n; --n)
81 inb(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Hans Verkuilc32a9d72009-03-06 13:53:26 -030084static void outbits(int bits, unsigned int data, int nWait, int io)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 int bit;
Hans Verkuilc32a9d72009-03-06 13:53:26 -030087
88 for (; --bits >= 0;) {
89 bit = (data >> bits) & 1;
90 outb(bit, io);
91 wait(nWait, io);
92 outb(bit | 2, io);
93 wait(nWait, io);
94 outb(bit, io);
95 wait(nWait, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97}
98
Hans Verkuilc32a9d72009-03-06 13:53:26 -030099static inline void fmr2_mute(int io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300101 outb(0x00, io);
102 wait(4, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300105static inline void fmr2_unmute(int io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300107 outb(0x04, io);
108 wait(4, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300111static inline int fmr2_stereo_mode(int io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300113 int n = inb(io);
114
115 outb(6, io);
116 inb(io);
117 n = ((n >> 3) & 1) ^ 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 debug_print((KERN_DEBUG "stereo: %d\n", n));
119 return n;
120}
121
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300122static int fmr2_product_info(struct fmr2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300124 int n = inb(dev->io);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 n &= 0xC1;
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300127 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /* this should support volume set */
129 dev->card_type = 12;
130 return 0;
131 }
132 /* not volume (mine is 11) */
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300133 dev->card_type = (n == 128) ? 11 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return n;
135}
136
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300137static inline int fmr2_getsigstr(struct fmr2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300139 /* !!! works only if scanning freq */
140 int res = 0xffff;
141
142 outb(5, dev->io);
143 wait(4, dev->io);
144 if (!(inb(dev->io) & 1))
145 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 debug_print((KERN_DEBUG "signal: %d\n", res));
147 return res;
148}
149
150/* set frequency and unmute card */
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300151static int fmr2_setfreq(struct fmr2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 unsigned long freq = dev->curfreq;
154
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300155 fmr2_mute(dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /* 0x42 for mono output
158 * 0x102 forward scanning
159 * 0x182 scansione avanti
160 */
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300161 outbits(9, 0x2, 3, dev->io);
162 outbits(16, RSF16_ENCODE(freq), 2, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300164 fmr2_unmute(dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /* wait 0.11 sec */
167 msleep(110);
168
169 /* NOTE if mute this stop radio
170 you must set freq on unmute */
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300171 dev->stereo = fmr2_stereo_mode(dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173}
174
175/* !!! not tested, in my card this does't work !!! */
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300176static int fmr2_setvolume(struct fmr2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300178 int vol[16] = { 0x021, 0x084, 0x090, 0x104,
179 0x110, 0x204, 0x210, 0x402,
180 0x404, 0x408, 0x410, 0x801,
181 0x802, 0x804, 0x808, 0x810 };
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300182 int i, a;
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300183 int n = vol[dev->curvol & 0x0f];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300185 if (dev->card_type != 11)
186 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300188 for (i = 12; --i >= 0; ) {
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300189 a = ((n >> i) & 1) << 6; /* if (a==0) a = 0; else a = 0x40; */
190 outb(a | 4, dev->io);
191 wait(4, dev->io);
192 outb(a | 0x24, dev->io);
193 wait(4, dev->io);
194 outb(a | 4, dev->io);
195 wait(4, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300197 for (i = 6; --i >= 0; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 a = ((0x18 >> i) & 1) << 6;
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300199 outb(a | 4, dev->io);
200 wait(4, dev->io);
201 outb(a | 0x24, dev->io);
202 wait(4, dev->io);
203 outb(a | 4, dev->io);
204 wait(4, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300206 wait(4, dev->io);
207 outb(0x14, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209}
210
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300211static int vidioc_querycap(struct file *file, void *priv,
212 struct v4l2_capability *v)
213{
214 strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
215 strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300216 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300217 v->version = RADIO_VERSION;
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300218 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300219 return 0;
220}
221
222static int vidioc_g_tuner(struct file *file, void *priv,
223 struct v4l2_tuner *v)
224{
225 int mult;
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300226 struct fmr2 *fmr2 = video_drvdata(file);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300227
228 if (v->index > 0)
229 return -EINVAL;
230
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300231 strlcpy(v->name, "FM", sizeof(v->name));
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300232 v->type = V4L2_TUNER_RADIO;
233
234 mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300235 v->rangelow = RSF16_MINFREQ / mult;
236 v->rangehigh = RSF16_MAXFREQ / mult;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300237 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
238 v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW;
239 v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
240 V4L2_TUNER_MODE_MONO;
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300241 mutex_lock(&fmr2->lock);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300242 v->signal = fmr2_getsigstr(fmr2);
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300243 mutex_unlock(&fmr2->lock);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300244 return 0;
245}
246
247static int vidioc_s_tuner(struct file *file, void *priv,
248 struct v4l2_tuner *v)
249{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300250 return v->index ? -EINVAL : 0;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300251}
252
253static int vidioc_s_frequency(struct file *file, void *priv,
254 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300256 struct fmr2 *fmr2 = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300258 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
259 f->frequency *= 1000;
260 if (f->frequency < RSF16_MINFREQ ||
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300261 f->frequency > RSF16_MAXFREQ)
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300262 return -EINVAL;
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300263 /* rounding in steps of 200 to match the freq
264 that will be used */
265 fmr2->curfreq = (f->frequency / 200) * 200;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300266
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300267 /* set card freq (if not muted) */
268 if (fmr2->curvol && !fmr2->mute) {
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300269 mutex_lock(&fmr2->lock);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300270 fmr2_setfreq(fmr2);
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300271 mutex_unlock(&fmr2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300276static int vidioc_g_frequency(struct file *file, void *priv,
277 struct v4l2_frequency *f)
278{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300279 struct fmr2 *fmr2 = video_drvdata(file);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300280
281 f->type = V4L2_TUNER_RADIO;
282 f->frequency = fmr2->curfreq;
283 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
284 f->frequency /= 1000;
285 return 0;
286}
287
288static int vidioc_queryctrl(struct file *file, void *priv,
289 struct v4l2_queryctrl *qc)
290{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300291 struct fmr2 *fmr2 = video_drvdata(file);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300292
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300293 switch (qc->id) {
294 case V4L2_CID_AUDIO_MUTE:
295 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
296 case V4L2_CID_AUDIO_VOLUME:
297 /* Only card_type == 11 implements volume */
298 if (fmr2->card_type == 11)
299 return v4l2_ctrl_query_fill(qc, 0, 15, 1, 0);
300 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300301 }
302 return -EINVAL;
303}
304
305static int vidioc_g_ctrl(struct file *file, void *priv,
306 struct v4l2_control *ctrl)
307{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300308 struct fmr2 *fmr2 = video_drvdata(file);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300309
310 switch (ctrl->id) {
311 case V4L2_CID_AUDIO_MUTE:
312 ctrl->value = fmr2->mute;
313 return 0;
314 case V4L2_CID_AUDIO_VOLUME:
315 ctrl->value = fmr2->curvol;
316 return 0;
317 }
318 return -EINVAL;
319}
320
321static int vidioc_s_ctrl(struct file *file, void *priv,
322 struct v4l2_control *ctrl)
323{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300324 struct fmr2 *fmr2 = video_drvdata(file);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300325
326 switch (ctrl->id) {
327 case V4L2_CID_AUDIO_MUTE:
328 fmr2->mute = ctrl->value;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300329 break;
330 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300331 fmr2->curvol = ctrl->value;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300332 break;
333 default:
334 return -EINVAL;
335 }
336
337#ifdef DEBUG
338 if (fmr2->curvol && !fmr2->mute)
339 printk(KERN_DEBUG "unmute\n");
340 else
341 printk(KERN_DEBUG "mute\n");
342#endif
343
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300344 mutex_lock(&fmr2->lock);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300345 if (fmr2->curvol && !fmr2->mute) {
346 fmr2_setvolume(fmr2);
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300347 /* Set frequency and unmute card */
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300348 fmr2_setfreq(fmr2);
349 } else
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300350 fmr2_mute(fmr2->io);
351 mutex_unlock(&fmr2->lock);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300352 return 0;
353}
354
355static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
356{
357 *i = 0;
358 return 0;
359}
360
361static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
362{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300363 return i ? -EINVAL : 0;
364}
365
366static int vidioc_g_audio(struct file *file, void *priv,
367 struct v4l2_audio *a)
368{
369 a->index = 0;
370 strlcpy(a->name, "Radio", sizeof(a->name));
371 a->capability = V4L2_AUDCAP_STEREO;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300372 return 0;
373}
374
375static int vidioc_s_audio(struct file *file, void *priv,
376 struct v4l2_audio *a)
377{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300378 return a->index ? -EINVAL : 0;
379}
380
381static int fmr2_open(struct file *file)
382{
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300386static int fmr2_release(struct file *file)
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300387{
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300388 return 0;
389}
390
Hans Verkuilbec43662008-12-30 06:58:20 -0300391static const struct v4l2_file_operations fmr2_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 .owner = THIS_MODULE,
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300393 .open = fmr2_open,
394 .release = fmr2_release,
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300395 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
Hans Verkuila3998102008-07-21 02:57:38 -0300398static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300399 .vidioc_querycap = vidioc_querycap,
400 .vidioc_g_tuner = vidioc_g_tuner,
401 .vidioc_s_tuner = vidioc_s_tuner,
402 .vidioc_g_audio = vidioc_g_audio,
403 .vidioc_s_audio = vidioc_s_audio,
404 .vidioc_g_input = vidioc_g_input,
405 .vidioc_s_input = vidioc_s_input,
406 .vidioc_g_frequency = vidioc_g_frequency,
407 .vidioc_s_frequency = vidioc_s_frequency,
408 .vidioc_queryctrl = vidioc_queryctrl,
409 .vidioc_g_ctrl = vidioc_g_ctrl,
410 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411};
412
413static int __init fmr2_init(void)
414{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300415 struct fmr2 *fmr2 = &fmr2_card;
416 struct v4l2_device *v4l2_dev = &fmr2->v4l2_dev;
417 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300419 strlcpy(v4l2_dev->name, "sf16fmr2", sizeof(v4l2_dev->name));
420 fmr2->io = io;
421 fmr2->stereo = 1;
422 fmr2->flags = V4L2_TUNER_CAP_LOW;
423 mutex_init(&fmr2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300425 if (!request_region(fmr2->io, 2, "sf16fmr2")) {
426 v4l2_err(v4l2_dev, "request_region failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -EBUSY;
428 }
429
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300430 res = v4l2_device_register(NULL, v4l2_dev);
431 if (res < 0) {
432 release_region(fmr2->io, 2);
433 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
434 return res;
435 }
436
437 strlcpy(fmr2->vdev.name, v4l2_dev->name, sizeof(fmr2->vdev.name));
438 fmr2->vdev.v4l2_dev = v4l2_dev;
439 fmr2->vdev.fops = &fmr2_fops;
440 fmr2->vdev.ioctl_ops = &fmr2_ioctl_ops;
441 fmr2->vdev.release = video_device_release_empty;
442 video_set_drvdata(&fmr2->vdev, fmr2);
443
444 if (video_register_device(&fmr2->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
445 v4l2_device_unregister(v4l2_dev);
446 release_region(fmr2->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -EINVAL;
448 }
449
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300450 v4l2_info(v4l2_dev, "SF16FMR2 radio card driver at 0x%x.\n", fmr2->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* mute card - prevents noisy bootups */
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300452 mutex_lock(&fmr2->lock);
453 fmr2_mute(fmr2->io);
454 fmr2_product_info(fmr2);
455 mutex_unlock(&fmr2->lock);
456 debug_print((KERN_DEBUG "card_type %d\n", fmr2->card_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300460static void __exit fmr2_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300462 struct fmr2 *fmr2 = &fmr2_card;
463
464 video_unregister_device(&fmr2->vdev);
465 v4l2_device_unregister(&fmr2->v4l2_dev);
466 release_region(fmr2->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469module_init(fmr2_init);
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300470module_exit(fmr2_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472#ifndef MODULE
473
474static int __init fmr2_setup_io(char *str)
475{
476 get_option(&str, &io);
477 return 1;
478}
479
480__setup("sf16fmr2=", fmr2_setup_io);
481
482#endif