]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/radio/radio-sf16fmi.c
ipv4: convert dst_metrics.refcnt from atomic_t to refcount_t
[mirror_ubuntu-artful-kernel.git] / drivers / media / radio / radio-sf16fmi.c
1 /* SF16-FMI, SF16-FMP and SF16-FMD 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@lxorguk.ukuu.org.uk>
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 SF16-FMI/SF16-FMP/SF16-FMD
15 *
16 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
17 */
18
19 #include <linux/kernel.h> /* __setup */
20 #include <linux/module.h> /* Modules */
21 #include <linux/init.h> /* Initdata */
22 #include <linux/ioport.h> /* request_region */
23 #include <linux/delay.h> /* udelay */
24 #include <linux/isapnp.h>
25 #include <linux/mutex.h>
26 #include <linux/videodev2.h> /* kernel radio structs */
27 #include <linux/io.h> /* outb, outb_p */
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-event.h>
32 #include "lm7000.h"
33
34 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
35 MODULE_DESCRIPTION("A driver for the SF16-FMI, SF16-FMP and SF16-FMD radio.");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION("0.0.3");
38
39 static int io = -1;
40 static int radio_nr = -1;
41
42 module_param(io, int, 0);
43 MODULE_PARM_DESC(io, "I/O address of the SF16-FMI/SF16-FMP/SF16-FMD card (0x284 or 0x384)");
44 module_param(radio_nr, int, 0);
45
46 struct fmi
47 {
48 struct v4l2_device v4l2_dev;
49 struct v4l2_ctrl_handler hdl;
50 struct video_device vdev;
51 int io;
52 bool mute;
53 u32 curfreq; /* freq in kHz */
54 struct mutex lock;
55 };
56
57 static struct fmi fmi_card;
58 static struct pnp_dev *dev;
59 static bool pnp_attached;
60
61 #define RSF16_MINFREQ (87U * 16000)
62 #define RSF16_MAXFREQ (108U * 16000)
63
64 #define FMI_BIT_TUN_CE (1 << 0)
65 #define FMI_BIT_TUN_CLK (1 << 1)
66 #define FMI_BIT_TUN_DATA (1 << 2)
67 #define FMI_BIT_VOL_SW (1 << 3)
68 #define FMI_BIT_TUN_STRQ (1 << 4)
69
70 static void fmi_set_pins(void *handle, u8 pins)
71 {
72 struct fmi *fmi = handle;
73 u8 bits = FMI_BIT_TUN_STRQ;
74
75 if (!fmi->mute)
76 bits |= FMI_BIT_VOL_SW;
77
78 if (pins & LM7000_DATA)
79 bits |= FMI_BIT_TUN_DATA;
80 if (pins & LM7000_CLK)
81 bits |= FMI_BIT_TUN_CLK;
82 if (pins & LM7000_CE)
83 bits |= FMI_BIT_TUN_CE;
84
85 mutex_lock(&fmi->lock);
86 outb_p(bits, fmi->io);
87 mutex_unlock(&fmi->lock);
88 }
89
90 static inline void fmi_mute(struct fmi *fmi)
91 {
92 mutex_lock(&fmi->lock);
93 outb(0x00, fmi->io);
94 mutex_unlock(&fmi->lock);
95 }
96
97 static inline void fmi_unmute(struct fmi *fmi)
98 {
99 mutex_lock(&fmi->lock);
100 outb(0x08, fmi->io);
101 mutex_unlock(&fmi->lock);
102 }
103
104 static inline int fmi_getsigstr(struct fmi *fmi)
105 {
106 int val;
107 int res;
108
109 mutex_lock(&fmi->lock);
110 val = fmi->mute ? 0x00 : 0x08; /* mute/unmute */
111 outb(val, fmi->io);
112 outb(val | 0x10, fmi->io);
113 msleep(143); /* was schedule_timeout(HZ/7) */
114 res = (int)inb(fmi->io + 1);
115 outb(val, fmi->io);
116
117 mutex_unlock(&fmi->lock);
118 return (res & 2) ? 0 : 0xFFFF;
119 }
120
121 static void fmi_set_freq(struct fmi *fmi)
122 {
123 fmi->curfreq = clamp(fmi->curfreq, RSF16_MINFREQ, RSF16_MAXFREQ);
124 /* rounding in steps of 800 to match the freq
125 that will be used */
126 lm7000_set_freq((fmi->curfreq / 800) * 800, fmi, fmi_set_pins);
127 }
128
129 static int vidioc_querycap(struct file *file, void *priv,
130 struct v4l2_capability *v)
131 {
132 strlcpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
133 strlcpy(v->card, "SF16-FMI/FMP/FMD radio", sizeof(v->card));
134 strlcpy(v->bus_info, "ISA:radio-sf16fmi", sizeof(v->bus_info));
135 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
136 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
137 return 0;
138 }
139
140 static int vidioc_g_tuner(struct file *file, void *priv,
141 struct v4l2_tuner *v)
142 {
143 struct fmi *fmi = video_drvdata(file);
144
145 if (v->index > 0)
146 return -EINVAL;
147
148 strlcpy(v->name, "FM", sizeof(v->name));
149 v->type = V4L2_TUNER_RADIO;
150 v->rangelow = RSF16_MINFREQ;
151 v->rangehigh = RSF16_MAXFREQ;
152 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
153 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
154 v->audmode = V4L2_TUNER_MODE_STEREO;
155 v->signal = fmi_getsigstr(fmi);
156 return 0;
157 }
158
159 static int vidioc_s_tuner(struct file *file, void *priv,
160 const struct v4l2_tuner *v)
161 {
162 return v->index ? -EINVAL : 0;
163 }
164
165 static int vidioc_s_frequency(struct file *file, void *priv,
166 const struct v4l2_frequency *f)
167 {
168 struct fmi *fmi = video_drvdata(file);
169
170 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
171 return -EINVAL;
172
173 fmi->curfreq = f->frequency;
174 fmi_set_freq(fmi);
175
176 return 0;
177 }
178
179 static int vidioc_g_frequency(struct file *file, void *priv,
180 struct v4l2_frequency *f)
181 {
182 struct fmi *fmi = video_drvdata(file);
183
184 if (f->tuner != 0)
185 return -EINVAL;
186 f->type = V4L2_TUNER_RADIO;
187 f->frequency = fmi->curfreq;
188 return 0;
189 }
190
191 static int fmi_s_ctrl(struct v4l2_ctrl *ctrl)
192 {
193 struct fmi *fmi = container_of(ctrl->handler, struct fmi, hdl);
194
195 switch (ctrl->id) {
196 case V4L2_CID_AUDIO_MUTE:
197 if (ctrl->val)
198 fmi_mute(fmi);
199 else
200 fmi_unmute(fmi);
201 fmi->mute = ctrl->val;
202 return 0;
203 }
204 return -EINVAL;
205 }
206
207 static const struct v4l2_ctrl_ops fmi_ctrl_ops = {
208 .s_ctrl = fmi_s_ctrl,
209 };
210
211 static const struct v4l2_file_operations fmi_fops = {
212 .owner = THIS_MODULE,
213 .open = v4l2_fh_open,
214 .release = v4l2_fh_release,
215 .poll = v4l2_ctrl_poll,
216 .unlocked_ioctl = video_ioctl2,
217 };
218
219 static const struct v4l2_ioctl_ops fmi_ioctl_ops = {
220 .vidioc_querycap = vidioc_querycap,
221 .vidioc_g_tuner = vidioc_g_tuner,
222 .vidioc_s_tuner = vidioc_s_tuner,
223 .vidioc_g_frequency = vidioc_g_frequency,
224 .vidioc_s_frequency = vidioc_s_frequency,
225 .vidioc_log_status = v4l2_ctrl_log_status,
226 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
227 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
228 };
229
230 /* ladis: this is my card. does any other types exist? */
231 static struct isapnp_device_id id_table[] = {
232 /* SF16-FMI */
233 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
234 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
235 /* SF16-FMD */
236 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
237 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0},
238 { ISAPNP_CARD_END, },
239 };
240
241 MODULE_DEVICE_TABLE(isapnp, id_table);
242
243 static int __init isapnp_fmi_probe(void)
244 {
245 int i = 0;
246
247 while (id_table[i].card_vendor != 0 && dev == NULL) {
248 dev = pnp_find_dev(NULL, id_table[i].vendor,
249 id_table[i].function, NULL);
250 i++;
251 }
252
253 if (!dev)
254 return -ENODEV;
255 if (pnp_device_attach(dev) < 0)
256 return -EAGAIN;
257 if (pnp_activate_dev(dev) < 0) {
258 printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n");
259 pnp_device_detach(dev);
260 return -ENOMEM;
261 }
262 if (!pnp_port_valid(dev, 0)) {
263 pnp_device_detach(dev);
264 return -ENODEV;
265 }
266
267 i = pnp_port_start(dev, 0);
268 printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i);
269
270 return i;
271 }
272
273 static int __init fmi_init(void)
274 {
275 struct fmi *fmi = &fmi_card;
276 struct v4l2_device *v4l2_dev = &fmi->v4l2_dev;
277 struct v4l2_ctrl_handler *hdl = &fmi->hdl;
278 int res, i;
279 int probe_ports[] = { 0, 0x284, 0x384 };
280
281 if (io < 0) {
282 for (i = 0; i < ARRAY_SIZE(probe_ports); i++) {
283 io = probe_ports[i];
284 if (io == 0) {
285 io = isapnp_fmi_probe();
286 if (io < 0)
287 continue;
288 pnp_attached = true;
289 }
290 if (!request_region(io, 2, "radio-sf16fmi")) {
291 if (pnp_attached)
292 pnp_device_detach(dev);
293 io = -1;
294 continue;
295 }
296 if (pnp_attached ||
297 ((inb(io) & 0xf9) == 0xf9 && (inb(io) & 0x4) == 0))
298 break;
299 release_region(io, 2);
300 io = -1;
301 }
302 } else {
303 if (!request_region(io, 2, "radio-sf16fmi")) {
304 printk(KERN_ERR "radio-sf16fmi: port %#x already in use\n", io);
305 return -EBUSY;
306 }
307 if (inb(io) == 0xff) {
308 printk(KERN_ERR "radio-sf16fmi: card not present at %#x\n", io);
309 release_region(io, 2);
310 return -ENODEV;
311 }
312 }
313 if (io < 0) {
314 printk(KERN_ERR "radio-sf16fmi: no cards found\n");
315 return -ENODEV;
316 }
317
318 strlcpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name));
319 fmi->io = io;
320
321 res = v4l2_device_register(NULL, v4l2_dev);
322 if (res < 0) {
323 release_region(fmi->io, 2);
324 if (pnp_attached)
325 pnp_device_detach(dev);
326 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
327 return res;
328 }
329
330 v4l2_ctrl_handler_init(hdl, 1);
331 v4l2_ctrl_new_std(hdl, &fmi_ctrl_ops,
332 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
333 v4l2_dev->ctrl_handler = hdl;
334 if (hdl->error) {
335 res = hdl->error;
336 v4l2_err(v4l2_dev, "Could not register controls\n");
337 v4l2_ctrl_handler_free(hdl);
338 v4l2_device_unregister(v4l2_dev);
339 return res;
340 }
341
342 strlcpy(fmi->vdev.name, v4l2_dev->name, sizeof(fmi->vdev.name));
343 fmi->vdev.v4l2_dev = v4l2_dev;
344 fmi->vdev.fops = &fmi_fops;
345 fmi->vdev.ioctl_ops = &fmi_ioctl_ops;
346 fmi->vdev.release = video_device_release_empty;
347 video_set_drvdata(&fmi->vdev, fmi);
348
349 mutex_init(&fmi->lock);
350
351 /* mute card and set default frequency */
352 fmi->mute = true;
353 fmi->curfreq = RSF16_MINFREQ;
354 fmi_set_freq(fmi);
355
356 if (video_register_device(&fmi->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
357 v4l2_ctrl_handler_free(hdl);
358 v4l2_device_unregister(v4l2_dev);
359 release_region(fmi->io, 2);
360 if (pnp_attached)
361 pnp_device_detach(dev);
362 return -EINVAL;
363 }
364
365 v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io);
366 return 0;
367 }
368
369 static void __exit fmi_exit(void)
370 {
371 struct fmi *fmi = &fmi_card;
372
373 v4l2_ctrl_handler_free(&fmi->hdl);
374 video_unregister_device(&fmi->vdev);
375 v4l2_device_unregister(&fmi->v4l2_dev);
376 release_region(fmi->io, 2);
377 if (dev && pnp_attached)
378 pnp_device_detach(dev);
379 }
380
381 module_init(fmi_init);
382 module_exit(fmi_exit);