V4L/DVB (8786): v4l2: remove the priv field, use dev_get_drvdata instead
[deliverable/linux.git] / drivers / media / radio / radio-sf16fmi.c
1 /* SF16FMI radio driver for Linux radio support
2 * heavily based on rtrack driver...
3 * (c) 1997 M. Kirkwood
4 * (c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz
5 *
6 * Fitted to new interface by Alan Cox <alan.cox@linux.org>
7 * Made working and cleaned up functions <mikael.hedin@irf.se>
8 * Support for ISAPnP by Ladislav Michl <ladis@psi.cz>
9 *
10 * Notes on the hardware
11 *
12 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
13 * No volume control - only mute/unmute - you have to use line volume
14 * control on SB-part of SF16FMI
15 *
16 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
17 */
18
19 #include <linux/version.h>
20 #include <linux/kernel.h> /* __setup */
21 #include <linux/module.h> /* Modules */
22 #include <linux/init.h> /* Initdata */
23 #include <linux/ioport.h> /* request_region */
24 #include <linux/delay.h> /* udelay */
25 #include <linux/videodev2.h> /* kernel radio structs */
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ioctl.h>
28 #include <linux/isapnp.h>
29 #include <asm/io.h> /* outb, outb_p */
30 #include <asm/uaccess.h> /* copy to/from user */
31 #include <linux/mutex.h>
32
33 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
34
35 static struct v4l2_queryctrl radio_qctrl[] = {
36 {
37 .id = V4L2_CID_AUDIO_MUTE,
38 .name = "Mute",
39 .minimum = 0,
40 .maximum = 1,
41 .default_value = 1,
42 .type = V4L2_CTRL_TYPE_BOOLEAN,
43 }
44 };
45
46 struct fmi_device
47 {
48 unsigned long in_use;
49 int port;
50 int curvol; /* 1 or 0 */
51 unsigned long curfreq; /* freq in kHz */
52 __u32 flags;
53 };
54
55 static int io = -1;
56 static int radio_nr = -1;
57 static struct pnp_dev *dev = NULL;
58 static struct mutex lock;
59
60 /* freq is in 1/16 kHz to internal number, hw precision is 50 kHz */
61 /* It is only useful to give freq in intervall of 800 (=0.05Mhz),
62 * other bits will be truncated, e.g 92.7400016 -> 92.7, but
63 * 92.7400017 -> 92.75
64 */
65 #define RSF16_ENCODE(x) ((x)/800+214)
66 #define RSF16_MINFREQ 87*16000
67 #define RSF16_MAXFREQ 108*16000
68
69 static void outbits(int bits, unsigned int data, int port)
70 {
71 while(bits--) {
72 if(data & 1) {
73 outb(5, port);
74 udelay(6);
75 outb(7, port);
76 udelay(6);
77 } else {
78 outb(1, port);
79 udelay(6);
80 outb(3, port);
81 udelay(6);
82 }
83 data>>=1;
84 }
85 }
86
87 static inline void fmi_mute(int port)
88 {
89 mutex_lock(&lock);
90 outb(0x00, port);
91 mutex_unlock(&lock);
92 }
93
94 static inline void fmi_unmute(int port)
95 {
96 mutex_lock(&lock);
97 outb(0x08, port);
98 mutex_unlock(&lock);
99 }
100
101 static inline int fmi_setfreq(struct fmi_device *dev)
102 {
103 int myport = dev->port;
104 unsigned long freq = dev->curfreq;
105
106 mutex_lock(&lock);
107
108 outbits(16, RSF16_ENCODE(freq), myport);
109 outbits(8, 0xC0, myport);
110 msleep(143); /* was schedule_timeout(HZ/7) */
111 mutex_unlock(&lock);
112 if (dev->curvol) fmi_unmute(myport);
113 return 0;
114 }
115
116 static inline int fmi_getsigstr(struct fmi_device *dev)
117 {
118 int val;
119 int res;
120 int myport = dev->port;
121
122
123 mutex_lock(&lock);
124 val = dev->curvol ? 0x08 : 0x00; /* unmute/mute */
125 outb(val, myport);
126 outb(val | 0x10, myport);
127 msleep(143); /* was schedule_timeout(HZ/7) */
128 res = (int)inb(myport+1);
129 outb(val, myport);
130
131 mutex_unlock(&lock);
132 return (res & 2) ? 0 : 0xFFFF;
133 }
134
135 static int vidioc_querycap(struct file *file, void *priv,
136 struct v4l2_capability *v)
137 {
138 strlcpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
139 strlcpy(v->card, "SF16-FMx radio", sizeof(v->card));
140 sprintf(v->bus_info, "ISA");
141 v->version = RADIO_VERSION;
142 v->capabilities = V4L2_CAP_TUNER;
143 return 0;
144 }
145
146 static int vidioc_g_tuner(struct file *file, void *priv,
147 struct v4l2_tuner *v)
148 {
149 int mult;
150 struct video_device *dev = video_devdata(file);
151 struct fmi_device *fmi = video_get_drvdata(dev);
152
153 if (v->index > 0)
154 return -EINVAL;
155
156 strcpy(v->name, "FM");
157 v->type = V4L2_TUNER_RADIO;
158 mult = (fmi->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
159 v->rangelow = RSF16_MINFREQ/mult;
160 v->rangehigh = RSF16_MAXFREQ/mult;
161 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
162 v->capability = fmi->flags&V4L2_TUNER_CAP_LOW;
163 v->audmode = V4L2_TUNER_MODE_STEREO;
164 v->signal = fmi_getsigstr(fmi);
165 return 0;
166 }
167
168 static int vidioc_s_tuner(struct file *file, void *priv,
169 struct v4l2_tuner *v)
170 {
171 if (v->index > 0)
172 return -EINVAL;
173 return 0;
174 }
175
176 static int vidioc_s_frequency(struct file *file, void *priv,
177 struct v4l2_frequency *f)
178 {
179 struct video_device *dev = video_devdata(file);
180 struct fmi_device *fmi = video_get_drvdata(dev);
181
182 if (!(fmi->flags & V4L2_TUNER_CAP_LOW))
183 f->frequency *= 1000;
184 if (f->frequency < RSF16_MINFREQ ||
185 f->frequency > RSF16_MAXFREQ )
186 return -EINVAL;
187 /*rounding in steps of 800 to match th freq
188 that will be used */
189 fmi->curfreq = (f->frequency/800)*800;
190 fmi_setfreq(fmi);
191 return 0;
192 }
193
194 static int vidioc_g_frequency(struct file *file, void *priv,
195 struct v4l2_frequency *f)
196 {
197 struct video_device *dev = video_devdata(file);
198 struct fmi_device *fmi = video_get_drvdata(dev);
199
200 f->type = V4L2_TUNER_RADIO;
201 f->frequency = fmi->curfreq;
202 if (!(fmi->flags & V4L2_TUNER_CAP_LOW))
203 f->frequency /= 1000;
204 return 0;
205 }
206
207 static int vidioc_queryctrl(struct file *file, void *priv,
208 struct v4l2_queryctrl *qc)
209 {
210 int i;
211
212 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
213 if (qc->id && qc->id == radio_qctrl[i].id) {
214 memcpy(qc, &(radio_qctrl[i]),
215 sizeof(*qc));
216 return 0;
217 }
218 }
219 return -EINVAL;
220 }
221
222 static int vidioc_g_ctrl(struct file *file, void *priv,
223 struct v4l2_control *ctrl)
224 {
225 struct video_device *dev = video_devdata(file);
226 struct fmi_device *fmi = video_get_drvdata(dev);
227
228 switch (ctrl->id) {
229 case V4L2_CID_AUDIO_MUTE:
230 ctrl->value = fmi->curvol;
231 return 0;
232 }
233 return -EINVAL;
234 }
235
236 static int vidioc_s_ctrl(struct file *file, void *priv,
237 struct v4l2_control *ctrl)
238 {
239 struct video_device *dev = video_devdata(file);
240 struct fmi_device *fmi = video_get_drvdata(dev);
241
242 switch (ctrl->id) {
243 case V4L2_CID_AUDIO_MUTE:
244 if (ctrl->value)
245 fmi_mute(fmi->port);
246 else
247 fmi_unmute(fmi->port);
248 fmi->curvol = ctrl->value;
249 return 0;
250 }
251 return -EINVAL;
252 }
253
254 static int vidioc_g_audio(struct file *file, void *priv,
255 struct v4l2_audio *a)
256 {
257 if (a->index > 1)
258 return -EINVAL;
259
260 strcpy(a->name, "Radio");
261 a->capability = V4L2_AUDCAP_STEREO;
262 return 0;
263 }
264
265 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
266 {
267 *i = 0;
268 return 0;
269 }
270
271 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
272 {
273 if (i != 0)
274 return -EINVAL;
275 return 0;
276 }
277
278 static int vidioc_s_audio(struct file *file, void *priv,
279 struct v4l2_audio *a)
280 {
281 if (a->index != 0)
282 return -EINVAL;
283 return 0;
284 }
285
286 static struct fmi_device fmi_unit;
287
288 static int fmi_exclusive_open(struct inode *inode, struct file *file)
289 {
290 return test_and_set_bit(0, &fmi_unit.in_use) ? -EBUSY : 0;
291 }
292
293 static int fmi_exclusive_release(struct inode *inode, struct file *file)
294 {
295 clear_bit(0, &fmi_unit.in_use);
296 return 0;
297 }
298
299 static const struct file_operations fmi_fops = {
300 .owner = THIS_MODULE,
301 .open = fmi_exclusive_open,
302 .release = fmi_exclusive_release,
303 .ioctl = video_ioctl2,
304 #ifdef CONFIG_COMPAT
305 .compat_ioctl = v4l_compat_ioctl32,
306 #endif
307 .llseek = no_llseek,
308 };
309
310 static const struct v4l2_ioctl_ops fmi_ioctl_ops = {
311 .vidioc_querycap = vidioc_querycap,
312 .vidioc_g_tuner = vidioc_g_tuner,
313 .vidioc_s_tuner = vidioc_s_tuner,
314 .vidioc_g_audio = vidioc_g_audio,
315 .vidioc_s_audio = vidioc_s_audio,
316 .vidioc_g_input = vidioc_g_input,
317 .vidioc_s_input = vidioc_s_input,
318 .vidioc_g_frequency = vidioc_g_frequency,
319 .vidioc_s_frequency = vidioc_s_frequency,
320 .vidioc_queryctrl = vidioc_queryctrl,
321 .vidioc_g_ctrl = vidioc_g_ctrl,
322 .vidioc_s_ctrl = vidioc_s_ctrl,
323 };
324
325 static struct video_device fmi_radio = {
326 .name = "SF16FMx radio",
327 .fops = &fmi_fops,
328 .ioctl_ops = &fmi_ioctl_ops,
329 .release = video_device_release_empty,
330 };
331
332 /* ladis: this is my card. does any other types exist? */
333 static struct isapnp_device_id id_table[] __devinitdata = {
334 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
335 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
336 { ISAPNP_CARD_END, },
337 };
338
339 MODULE_DEVICE_TABLE(isapnp, id_table);
340
341 static int __init isapnp_fmi_probe(void)
342 {
343 int i = 0;
344
345 while (id_table[i].card_vendor != 0 && dev == NULL) {
346 dev = pnp_find_dev(NULL, id_table[i].vendor,
347 id_table[i].function, NULL);
348 i++;
349 }
350
351 if (!dev)
352 return -ENODEV;
353 if (pnp_device_attach(dev) < 0)
354 return -EAGAIN;
355 if (pnp_activate_dev(dev) < 0) {
356 printk ("radio-sf16fmi: PnP configure failed (out of resources?)\n");
357 pnp_device_detach(dev);
358 return -ENOMEM;
359 }
360 if (!pnp_port_valid(dev, 0)) {
361 pnp_device_detach(dev);
362 return -ENODEV;
363 }
364
365 i = pnp_port_start(dev, 0);
366 printk ("radio-sf16fmi: PnP reports card at %#x\n", i);
367
368 return i;
369 }
370
371 static int __init fmi_init(void)
372 {
373 if (io < 0)
374 io = isapnp_fmi_probe();
375 if (io < 0) {
376 printk(KERN_ERR "radio-sf16fmi: No PnP card found.\n");
377 return io;
378 }
379 if (!request_region(io, 2, "radio-sf16fmi")) {
380 printk(KERN_ERR "radio-sf16fmi: port 0x%x already in use\n", io);
381 pnp_device_detach(dev);
382 return -EBUSY;
383 }
384
385 fmi_unit.port = io;
386 fmi_unit.curvol = 0;
387 fmi_unit.curfreq = 0;
388 fmi_unit.flags = V4L2_TUNER_CAP_LOW;
389 video_set_drvdata(&fmi_radio, &fmi_unit);
390
391 mutex_init(&lock);
392
393 if (video_register_device(&fmi_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
394 release_region(io, 2);
395 return -EINVAL;
396 }
397
398 printk(KERN_INFO "SF16FMx radio card driver at 0x%x\n", io);
399 /* mute card - prevents noisy bootups */
400 fmi_mute(io);
401 return 0;
402 }
403
404 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
405 MODULE_DESCRIPTION("A driver for the SF16MI radio.");
406 MODULE_LICENSE("GPL");
407
408 module_param(io, int, 0);
409 MODULE_PARM_DESC(io, "I/O address of the SF16MI card (0x284 or 0x384)");
410 module_param(radio_nr, int, 0);
411
412 static void __exit fmi_cleanup_module(void)
413 {
414 video_unregister_device(&fmi_radio);
415 release_region(io, 2);
416 if (dev)
417 pnp_device_detach(dev);
418 }
419
420 module_init(fmi_init);
421 module_exit(fmi_cleanup_module);
This page took 0.060277 seconds and 5 git commands to generate.