Commit | Line | Data |
---|---|---|
abed623c | 1 | /* SF16-FMI, SF16-FMP and SF16-FMD 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 | |
abed623c | 14 | * control on SB-part of SF16-FMI/SF16-FMP/SF16-FMD |
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> | |
6b39023a | 30 | #include "lm7000.h" |
1da177e4 | 31 | |
c41269fd | 32 | MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood"); |
abed623c | 33 | MODULE_DESCRIPTION("A driver for the SF16-FMI, SF16-FMP and SF16-FMD radio."); |
c41269fd | 34 | MODULE_LICENSE("GPL"); |
29834c1a | 35 | MODULE_VERSION("0.0.3"); |
a2ef73af | 36 | |
c41269fd HV |
37 | static int io = -1; |
38 | static int radio_nr = -1; | |
39 | ||
40 | module_param(io, int, 0); | |
abed623c | 41 | MODULE_PARM_DESC(io, "I/O address of the SF16-FMI/SF16-FMP/SF16-FMD card (0x284 or 0x384)"); |
c41269fd HV |
42 | module_param(radio_nr, int, 0); |
43 | ||
c41269fd | 44 | struct fmi |
1da177e4 | 45 | { |
c41269fd HV |
46 | struct v4l2_device v4l2_dev; |
47 | struct video_device vdev; | |
48 | int io; | |
4b830374 | 49 | bool mute; |
4286c6f6 | 50 | unsigned long curfreq; /* freq in kHz */ |
c41269fd | 51 | struct mutex lock; |
1da177e4 LT |
52 | }; |
53 | ||
c41269fd HV |
54 | static struct fmi fmi_card; |
55 | static struct pnp_dev *dev; | |
67cabf50 | 56 | bool pnp_attached; |
1da177e4 | 57 | |
c41269fd HV |
58 | #define RSF16_MINFREQ (87 * 16000) |
59 | #define RSF16_MAXFREQ (108 * 16000) | |
1da177e4 | 60 | |
6b39023a OZ |
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 | ||
a5133b17 | 67 | static void fmi_set_pins(void *handle, u8 pins) |
1da177e4 | 68 | { |
6b39023a OZ |
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); | |
1da177e4 LT |
85 | } |
86 | ||
c41269fd | 87 | static inline void fmi_mute(struct fmi *fmi) |
1da177e4 | 88 | { |
c41269fd HV |
89 | mutex_lock(&fmi->lock); |
90 | outb(0x00, fmi->io); | |
91 | mutex_unlock(&fmi->lock); | |
1da177e4 LT |
92 | } |
93 | ||
c41269fd | 94 | static inline void fmi_unmute(struct fmi *fmi) |
1da177e4 | 95 | { |
c41269fd HV |
96 | mutex_lock(&fmi->lock); |
97 | outb(0x08, fmi->io); | |
98 | mutex_unlock(&fmi->lock); | |
1da177e4 LT |
99 | } |
100 | ||
c41269fd | 101 | static inline int fmi_getsigstr(struct fmi *fmi) |
1da177e4 LT |
102 | { |
103 | int val; | |
104 | int res; | |
4286c6f6 | 105 | |
c41269fd | 106 | mutex_lock(&fmi->lock); |
4b830374 | 107 | val = fmi->mute ? 0x00 : 0x08; /* mute/unmute */ |
c41269fd HV |
108 | outb(val, fmi->io); |
109 | outb(val | 0x10, fmi->io); | |
1da177e4 | 110 | msleep(143); /* was schedule_timeout(HZ/7) */ |
c41269fd HV |
111 | res = (int)inb(fmi->io + 1); |
112 | outb(val, fmi->io); | |
4286c6f6 | 113 | |
c41269fd | 114 | mutex_unlock(&fmi->lock); |
1da177e4 LT |
115 | return (res & 2) ? 0 : 0xFFFF; |
116 | } | |
117 | ||
c123b867 DL |
118 | static int vidioc_querycap(struct file *file, void *priv, |
119 | struct v4l2_capability *v) | |
1da177e4 | 120 | { |
c123b867 | 121 | strlcpy(v->driver, "radio-sf16fmi", sizeof(v->driver)); |
abed623c | 122 | strlcpy(v->card, "SF16-FMI/FMP/FMD radio", sizeof(v->card)); |
c41269fd | 123 | strlcpy(v->bus_info, "ISA", sizeof(v->bus_info)); |
c41269fd | 124 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
c123b867 DL |
125 | return 0; |
126 | } | |
127 | ||
128 | static int vidioc_g_tuner(struct file *file, void *priv, | |
129 | struct v4l2_tuner *v) | |
130 | { | |
c41269fd | 131 | struct fmi *fmi = video_drvdata(file); |
c123b867 DL |
132 | |
133 | if (v->index > 0) | |
134 | return -EINVAL; | |
135 | ||
c41269fd | 136 | strlcpy(v->name, "FM", sizeof(v->name)); |
c123b867 | 137 | v->type = V4L2_TUNER_RADIO; |
1d80db56 HV |
138 | v->rangelow = RSF16_MINFREQ; |
139 | v->rangehigh = RSF16_MAXFREQ; | |
dc47b778 | 140 | v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; |
38092a44 | 141 | v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW; |
c123b867 DL |
142 | v->audmode = V4L2_TUNER_MODE_STEREO; |
143 | v->signal = fmi_getsigstr(fmi); | |
144 | return 0; | |
145 | } | |
4286c6f6 | 146 | |
c123b867 DL |
147 | static int vidioc_s_tuner(struct file *file, void *priv, |
148 | struct v4l2_tuner *v) | |
149 | { | |
c41269fd | 150 | return v->index ? -EINVAL : 0; |
c123b867 | 151 | } |
a2ef73af | 152 | |
c123b867 | 153 | static int vidioc_s_frequency(struct file *file, void *priv, |
b530a447 | 154 | const struct v4l2_frequency *f) |
c123b867 | 155 | { |
c41269fd | 156 | struct fmi *fmi = video_drvdata(file); |
c123b867 | 157 | |
a3a9e287 HV |
158 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
159 | return -EINVAL; | |
c123b867 | 160 | if (f->frequency < RSF16_MINFREQ || |
c41269fd | 161 | f->frequency > RSF16_MAXFREQ) |
c123b867 | 162 | return -EINVAL; |
c41269fd HV |
163 | /* rounding in steps of 800 to match the freq |
164 | that will be used */ | |
6b39023a | 165 | lm7000_set_freq((f->frequency / 800) * 800, fmi, fmi_set_pins); |
c123b867 DL |
166 | return 0; |
167 | } | |
a2ef73af | 168 | |
c123b867 DL |
169 | static int vidioc_g_frequency(struct file *file, void *priv, |
170 | struct v4l2_frequency *f) | |
171 | { | |
c41269fd | 172 | struct fmi *fmi = video_drvdata(file); |
a2ef73af | 173 | |
a3a9e287 HV |
174 | if (f->tuner != 0) |
175 | return -EINVAL; | |
c123b867 DL |
176 | f->type = V4L2_TUNER_RADIO; |
177 | f->frequency = fmi->curfreq; | |
c123b867 DL |
178 | return 0; |
179 | } | |
a2ef73af | 180 | |
c123b867 DL |
181 | static int vidioc_queryctrl(struct file *file, void *priv, |
182 | struct v4l2_queryctrl *qc) | |
183 | { | |
c41269fd HV |
184 | switch (qc->id) { |
185 | case V4L2_CID_AUDIO_MUTE: | |
186 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); | |
c123b867 DL |
187 | } |
188 | return -EINVAL; | |
189 | } | |
a2ef73af | 190 | |
c123b867 DL |
191 | static int vidioc_g_ctrl(struct file *file, void *priv, |
192 | struct v4l2_control *ctrl) | |
193 | { | |
c41269fd | 194 | struct fmi *fmi = video_drvdata(file); |
a2ef73af | 195 | |
c123b867 DL |
196 | switch (ctrl->id) { |
197 | case V4L2_CID_AUDIO_MUTE: | |
4b830374 | 198 | ctrl->value = fmi->mute; |
c123b867 | 199 | return 0; |
1da177e4 | 200 | } |
c123b867 DL |
201 | return -EINVAL; |
202 | } | |
203 | ||
204 | static int vidioc_s_ctrl(struct file *file, void *priv, | |
205 | struct v4l2_control *ctrl) | |
206 | { | |
c41269fd | 207 | struct fmi *fmi = video_drvdata(file); |
c123b867 DL |
208 | |
209 | switch (ctrl->id) { | |
210 | case V4L2_CID_AUDIO_MUTE: | |
211 | if (ctrl->value) | |
c41269fd | 212 | fmi_mute(fmi); |
c123b867 | 213 | else |
c41269fd | 214 | fmi_unmute(fmi); |
4b830374 | 215 | fmi->mute = ctrl->value; |
c123b867 DL |
216 | return 0; |
217 | } | |
218 | return -EINVAL; | |
219 | } | |
220 | ||
c123b867 | 221 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
1da177e4 | 222 | { |
c123b867 DL |
223 | *i = 0; |
224 | return 0; | |
225 | } | |
226 | ||
227 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) | |
228 | { | |
c41269fd | 229 | return i ? -EINVAL : 0; |
c123b867 DL |
230 | } |
231 | ||
c41269fd | 232 | static int vidioc_g_audio(struct file *file, void *priv, |
c123b867 DL |
233 | struct v4l2_audio *a) |
234 | { | |
c41269fd HV |
235 | a->index = 0; |
236 | strlcpy(a->name, "Radio", sizeof(a->name)); | |
237 | a->capability = V4L2_AUDCAP_STEREO; | |
c123b867 | 238 | return 0; |
1da177e4 LT |
239 | } |
240 | ||
c41269fd | 241 | static int vidioc_s_audio(struct file *file, void *priv, |
0e8025b9 | 242 | const struct v4l2_audio *a) |
c41269fd HV |
243 | { |
244 | return a->index ? -EINVAL : 0; | |
245 | } | |
1da177e4 | 246 | |
bec43661 | 247 | static const struct v4l2_file_operations fmi_fops = { |
1da177e4 | 248 | .owner = THIS_MODULE, |
32958fdd | 249 | .unlocked_ioctl = video_ioctl2, |
1da177e4 LT |
250 | }; |
251 | ||
a399810c | 252 | static const struct v4l2_ioctl_ops fmi_ioctl_ops = { |
c123b867 DL |
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, | |
1da177e4 LT |
265 | }; |
266 | ||
267 | /* ladis: this is my card. does any other types exist? */ | |
4c62e976 | 268 | static struct isapnp_device_id id_table[] = { |
abed623c | 269 | /* SF16-FMI */ |
1da177e4 LT |
270 | { ISAPNP_ANY_ID, ISAPNP_ANY_ID, |
271 | ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0}, | |
abed623c OZ |
272 | /* SF16-FMD */ |
273 | { ISAPNP_ANY_ID, ISAPNP_ANY_ID, | |
274 | ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0}, | |
1da177e4 LT |
275 | { ISAPNP_CARD_END, }, |
276 | }; | |
277 | ||
278 | MODULE_DEVICE_TABLE(isapnp, id_table); | |
279 | ||
a999337b | 280 | static int __init isapnp_fmi_probe(void) |
1da177e4 LT |
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) { | |
c41269fd | 295 | printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n"); |
1da177e4 LT |
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); | |
c41269fd | 305 | printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i); |
1da177e4 LT |
306 | |
307 | return i; | |
308 | } | |
309 | ||
310 | static int __init fmi_init(void) | |
311 | { | |
c41269fd HV |
312 | struct fmi *fmi = &fmi_card; |
313 | struct v4l2_device *v4l2_dev = &fmi->v4l2_dev; | |
67cabf50 OZ |
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 | } | |
c41269fd | 353 | |
c41269fd HV |
354 | strlcpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name)); |
355 | fmi->io = io; | |
1da177e4 | 356 | |
c41269fd HV |
357 | res = v4l2_device_register(NULL, v4l2_dev); |
358 | if (res < 0) { | |
359 | release_region(fmi->io, 2); | |
67cabf50 OZ |
360 | if (pnp_attached) |
361 | pnp_device_detach(dev); | |
c41269fd HV |
362 | v4l2_err(v4l2_dev, "Could not register v4l2_device\n"); |
363 | return res; | |
364 | } | |
365 | ||
c41269fd HV |
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); | |
4286c6f6 | 372 | |
c41269fd | 373 | mutex_init(&fmi->lock); |
4286c6f6 | 374 | |
32958fdd HV |
375 | /* mute card - prevents noisy bootups */ |
376 | fmi_mute(fmi); | |
377 | ||
c41269fd HV |
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); | |
67cabf50 OZ |
381 | if (pnp_attached) |
382 | pnp_device_detach(dev); | |
1da177e4 LT |
383 | return -EINVAL; |
384 | } | |
4286c6f6 | 385 | |
c41269fd | 386 | v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io); |
1da177e4 LT |
387 | return 0; |
388 | } | |
389 | ||
c41269fd | 390 | static void __exit fmi_exit(void) |
1da177e4 | 391 | { |
c41269fd HV |
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); | |
67cabf50 | 397 | if (dev && pnp_attached) |
1da177e4 LT |
398 | pnp_device_detach(dev); |
399 | } | |
400 | ||
401 | module_init(fmi_init); | |
c41269fd | 402 | module_exit(fmi_exit); |