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