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