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