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