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