Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Typhoon Radio Card driver for radio support |
2 | * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de> | |
3 | * | |
1da177e4 LT |
4 | * Notes on the hardware |
5 | * | |
6 | * This card has two output sockets, one for speakers and one for line. | |
7 | * The speaker output has volume control, but only in four discrete | |
8 | * steps. The line output has neither volume control nor mute. | |
9 | * | |
10 | * The card has auto-stereo according to its manual, although it all | |
11 | * sounds mono to me (even with the Win/DOS drivers). Maybe it's my | |
12 | * antenna - I really don't know for sure. | |
13 | * | |
14 | * Frequency control is done digitally. | |
15 | * | |
16 | * Volume control is done digitally, but there are only four different | |
17 | * possible values. So you should better always turn the volume up and | |
18 | * use line control. I got the best results by connecting line output | |
19 | * to the sound card microphone input. For such a configuration the | |
20 | * volume control has no effect, since volume control only influences | |
21 | * the speaker output. | |
22 | * | |
23 | * There is no explicit mute/unmute. So I set the radio frequency to a | |
24 | * value where I do expect just noise and turn the speaker volume down. | |
25 | * The frequency change is necessary since the card never seems to be | |
26 | * completely silent. | |
30c48305 MCC |
27 | * |
28 | * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org> | |
1da177e4 LT |
29 | */ |
30 | ||
31 | #include <linux/module.h> /* Modules */ | |
32 | #include <linux/init.h> /* Initdata */ | |
fb911ee8 | 33 | #include <linux/ioport.h> /* request_region */ |
f1bfe89b | 34 | #include <linux/version.h> /* for KERNEL_VERSION MACRO */ |
30c48305 | 35 | #include <linux/videodev2.h> /* kernel radio structs */ |
f1bfe89b | 36 | #include <linux/io.h> /* outb, outb_p */ |
f1bfe89b | 37 | #include <media/v4l2-device.h> |
35ea11ff | 38 | #include <media/v4l2-ioctl.h> |
1da177e4 | 39 | |
f1bfe89b HV |
40 | MODULE_AUTHOR("Dr. Henrik Seidel"); |
41 | MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio)."); | |
42 | MODULE_LICENSE("GPL"); | |
1da177e4 LT |
43 | |
44 | #ifndef CONFIG_RADIO_TYPHOON_PORT | |
45 | #define CONFIG_RADIO_TYPHOON_PORT -1 | |
46 | #endif | |
47 | ||
48 | #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ | |
49 | #define CONFIG_RADIO_TYPHOON_MUTEFREQ 0 | |
50 | #endif | |
51 | ||
f1bfe89b HV |
52 | static int io = CONFIG_RADIO_TYPHOON_PORT; |
53 | static int radio_nr = -1; | |
1da177e4 | 54 | |
f1bfe89b HV |
55 | module_param(io, int, 0); |
56 | MODULE_PARM_DESC(io, "I/O address of the Typhoon card (0x316 or 0x336)"); | |
57 | ||
58 | module_param(radio_nr, int, 0); | |
59 | ||
60 | static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ; | |
61 | module_param(mutefreq, ulong, 0); | |
62 | MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)"); | |
63 | ||
64 | #define RADIO_VERSION KERNEL_VERSION(0, 1, 1) | |
65 | ||
66 | #define BANNER "Typhoon Radio Card driver v0.1.1\n" | |
67 | ||
68 | struct typhoon { | |
69 | struct v4l2_device v4l2_dev; | |
70 | struct video_device vdev; | |
71 | int io; | |
1da177e4 LT |
72 | int curvol; |
73 | int muted; | |
74 | unsigned long curfreq; | |
75 | unsigned long mutefreq; | |
3593cab5 | 76 | struct mutex lock; |
1da177e4 LT |
77 | }; |
78 | ||
f1bfe89b | 79 | static struct typhoon typhoon_card; |
1da177e4 | 80 | |
f1bfe89b | 81 | static void typhoon_setvol_generic(struct typhoon *dev, int vol) |
1da177e4 | 82 | { |
3593cab5 | 83 | mutex_lock(&dev->lock); |
1da177e4 LT |
84 | vol >>= 14; /* Map 16 bit to 2 bit */ |
85 | vol &= 3; | |
f1bfe89b HV |
86 | outb_p(vol / 2, dev->io); /* Set the volume, high bit. */ |
87 | outb_p(vol % 2, dev->io + 2); /* Set the volume, low bit. */ | |
3593cab5 | 88 | mutex_unlock(&dev->lock); |
1da177e4 LT |
89 | } |
90 | ||
f1bfe89b | 91 | static int typhoon_setfreq_generic(struct typhoon *dev, |
1da177e4 LT |
92 | unsigned long frequency) |
93 | { | |
94 | unsigned long outval; | |
95 | unsigned long x; | |
96 | ||
97 | /* | |
98 | * The frequency transfer curve is not linear. The best fit I could | |
99 | * get is | |
100 | * | |
101 | * outval = -155 + exp((f + 15.55) * 0.057)) | |
102 | * | |
103 | * where frequency f is in MHz. Since we don't have exp in the kernel, | |
104 | * I approximate this function by a third order polynomial. | |
105 | * | |
106 | */ | |
107 | ||
3593cab5 | 108 | mutex_lock(&dev->lock); |
1da177e4 LT |
109 | x = frequency / 160; |
110 | outval = (x * x + 2500) / 5000; | |
111 | outval = (outval * x + 5000) / 10000; | |
112 | outval -= (10 * x * x + 10433) / 20866; | |
113 | outval += 4 * x - 11505; | |
114 | ||
f1bfe89b HV |
115 | outb_p((outval >> 8) & 0x01, dev->io + 4); |
116 | outb_p(outval >> 9, dev->io + 6); | |
117 | outb_p(outval & 0xff, dev->io + 8); | |
3593cab5 | 118 | mutex_unlock(&dev->lock); |
1da177e4 LT |
119 | |
120 | return 0; | |
121 | } | |
122 | ||
f1bfe89b | 123 | static int typhoon_setfreq(struct typhoon *dev, unsigned long frequency) |
1da177e4 LT |
124 | { |
125 | typhoon_setfreq_generic(dev, frequency); | |
126 | dev->curfreq = frequency; | |
127 | return 0; | |
128 | } | |
129 | ||
f1bfe89b | 130 | static void typhoon_mute(struct typhoon *dev) |
1da177e4 LT |
131 | { |
132 | if (dev->muted == 1) | |
133 | return; | |
134 | typhoon_setvol_generic(dev, 0); | |
135 | typhoon_setfreq_generic(dev, dev->mutefreq); | |
136 | dev->muted = 1; | |
137 | } | |
138 | ||
f1bfe89b | 139 | static void typhoon_unmute(struct typhoon *dev) |
1da177e4 LT |
140 | { |
141 | if (dev->muted == 0) | |
142 | return; | |
143 | typhoon_setfreq_generic(dev, dev->curfreq); | |
144 | typhoon_setvol_generic(dev, dev->curvol); | |
145 | dev->muted = 0; | |
146 | } | |
147 | ||
f1bfe89b | 148 | static int typhoon_setvol(struct typhoon *dev, int vol) |
1da177e4 LT |
149 | { |
150 | if (dev->muted && vol != 0) { /* user is unmuting the card */ | |
151 | dev->curvol = vol; | |
152 | typhoon_unmute(dev); | |
153 | return 0; | |
154 | } | |
155 | if (vol == dev->curvol) /* requested volume == current */ | |
156 | return 0; | |
157 | ||
158 | if (vol == 0) { /* volume == 0 means mute the card */ | |
159 | typhoon_mute(dev); | |
160 | dev->curvol = vol; | |
161 | return 0; | |
162 | } | |
163 | typhoon_setvol_generic(dev, vol); | |
164 | dev->curvol = vol; | |
165 | return 0; | |
166 | } | |
167 | ||
3f6892ac DL |
168 | static int vidioc_querycap(struct file *file, void *priv, |
169 | struct v4l2_capability *v) | |
170 | { | |
171 | strlcpy(v->driver, "radio-typhoon", sizeof(v->driver)); | |
172 | strlcpy(v->card, "Typhoon Radio", sizeof(v->card)); | |
f1bfe89b | 173 | strlcpy(v->bus_info, "ISA", sizeof(v->bus_info)); |
3f6892ac | 174 | v->version = RADIO_VERSION; |
f1bfe89b | 175 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
3f6892ac DL |
176 | return 0; |
177 | } | |
178 | ||
179 | static int vidioc_g_tuner(struct file *file, void *priv, | |
180 | struct v4l2_tuner *v) | |
181 | { | |
182 | if (v->index > 0) | |
183 | return -EINVAL; | |
184 | ||
f1bfe89b | 185 | strlcpy(v->name, "FM", sizeof(v->name)); |
3f6892ac | 186 | v->type = V4L2_TUNER_RADIO; |
f1bfe89b HV |
187 | v->rangelow = 87.5 * 16000; |
188 | v->rangehigh = 108 * 16000; | |
3f6892ac DL |
189 | v->rxsubchans = V4L2_TUNER_SUB_MONO; |
190 | v->capability = V4L2_TUNER_CAP_LOW; | |
191 | v->audmode = V4L2_TUNER_MODE_MONO; | |
192 | v->signal = 0xFFFF; /* We can't get the signal strength */ | |
193 | return 0; | |
194 | } | |
1da177e4 | 195 | |
3f6892ac DL |
196 | static int vidioc_s_tuner(struct file *file, void *priv, |
197 | struct v4l2_tuner *v) | |
198 | { | |
f1bfe89b | 199 | return v->index ? -EINVAL : 0; |
3f6892ac DL |
200 | } |
201 | ||
f1bfe89b | 202 | static int vidioc_g_frequency(struct file *file, void *priv, |
3f6892ac | 203 | struct v4l2_frequency *f) |
1da177e4 | 204 | { |
f1bfe89b | 205 | struct typhoon *dev = video_drvdata(file); |
1da177e4 | 206 | |
a3a9e287 HV |
207 | if (f->tuner != 0) |
208 | return -EINVAL; | |
f1bfe89b HV |
209 | f->type = V4L2_TUNER_RADIO; |
210 | f->frequency = dev->curfreq; | |
3f6892ac DL |
211 | return 0; |
212 | } | |
30c48305 | 213 | |
f1bfe89b | 214 | static int vidioc_s_frequency(struct file *file, void *priv, |
3f6892ac DL |
215 | struct v4l2_frequency *f) |
216 | { | |
f1bfe89b | 217 | struct typhoon *dev = video_drvdata(file); |
30c48305 | 218 | |
a3a9e287 HV |
219 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
220 | return -EINVAL; | |
f1bfe89b HV |
221 | dev->curfreq = f->frequency; |
222 | typhoon_setfreq(dev, dev->curfreq); | |
3f6892ac DL |
223 | return 0; |
224 | } | |
30c48305 | 225 | |
3f6892ac DL |
226 | static int vidioc_queryctrl(struct file *file, void *priv, |
227 | struct v4l2_queryctrl *qc) | |
228 | { | |
f1bfe89b HV |
229 | switch (qc->id) { |
230 | case V4L2_CID_AUDIO_MUTE: | |
231 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); | |
232 | case V4L2_CID_AUDIO_VOLUME: | |
233 | return v4l2_ctrl_query_fill(qc, 0, 65535, 16384, 65535); | |
3f6892ac DL |
234 | } |
235 | return -EINVAL; | |
236 | } | |
30c48305 | 237 | |
3f6892ac DL |
238 | static int vidioc_g_ctrl(struct file *file, void *priv, |
239 | struct v4l2_control *ctrl) | |
240 | { | |
f1bfe89b | 241 | struct typhoon *dev = video_drvdata(file); |
30c48305 | 242 | |
3f6892ac DL |
243 | switch (ctrl->id) { |
244 | case V4L2_CID_AUDIO_MUTE: | |
f1bfe89b | 245 | ctrl->value = dev->muted; |
3f6892ac DL |
246 | return 0; |
247 | case V4L2_CID_AUDIO_VOLUME: | |
f1bfe89b | 248 | ctrl->value = dev->curvol; |
3f6892ac DL |
249 | return 0; |
250 | } | |
251 | return -EINVAL; | |
252 | } | |
30c48305 | 253 | |
3f6892ac DL |
254 | static int vidioc_s_ctrl (struct file *file, void *priv, |
255 | struct v4l2_control *ctrl) | |
256 | { | |
f1bfe89b | 257 | struct typhoon *dev = video_drvdata(file); |
30c48305 | 258 | |
3f6892ac DL |
259 | switch (ctrl->id) { |
260 | case V4L2_CID_AUDIO_MUTE: | |
261 | if (ctrl->value) | |
f1bfe89b | 262 | typhoon_mute(dev); |
3f6892ac | 263 | else |
f1bfe89b | 264 | typhoon_unmute(dev); |
3f6892ac DL |
265 | return 0; |
266 | case V4L2_CID_AUDIO_VOLUME: | |
f1bfe89b | 267 | typhoon_setvol(dev, ctrl->value); |
3f6892ac DL |
268 | return 0; |
269 | } | |
270 | return -EINVAL; | |
271 | } | |
30c48305 | 272 | |
3f6892ac DL |
273 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
274 | { | |
275 | *i = 0; | |
276 | return 0; | |
1da177e4 LT |
277 | } |
278 | ||
3f6892ac | 279 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
1da177e4 | 280 | { |
f1bfe89b HV |
281 | return i ? -EINVAL : 0; |
282 | } | |
283 | ||
284 | static int vidioc_g_audio(struct file *file, void *priv, | |
285 | struct v4l2_audio *a) | |
286 | { | |
287 | a->index = 0; | |
288 | strlcpy(a->name, "Radio", sizeof(a->name)); | |
289 | a->capability = V4L2_AUDCAP_STEREO; | |
3f6892ac DL |
290 | return 0; |
291 | } | |
292 | ||
293 | static int vidioc_s_audio(struct file *file, void *priv, | |
294 | struct v4l2_audio *a) | |
295 | { | |
f1bfe89b | 296 | return a->index ? -EINVAL : 0; |
1da177e4 LT |
297 | } |
298 | ||
f1bfe89b | 299 | static int vidioc_log_status(struct file *file, void *priv) |
1da177e4 | 300 | { |
f1bfe89b HV |
301 | struct typhoon *dev = video_drvdata(file); |
302 | struct v4l2_device *v4l2_dev = &dev->v4l2_dev; | |
303 | ||
304 | v4l2_info(v4l2_dev, BANNER); | |
305 | #ifdef MODULE | |
306 | v4l2_info(v4l2_dev, "Load type: Driver loaded as a module\n\n"); | |
307 | #else | |
308 | v4l2_info(v4l2_dev, "Load type: Driver compiled into kernel\n\n"); | |
309 | #endif | |
310 | v4l2_info(v4l2_dev, "frequency = %lu kHz\n", dev->curfreq >> 4); | |
311 | v4l2_info(v4l2_dev, "volume = %d\n", dev->curvol); | |
312 | v4l2_info(v4l2_dev, "mute = %s\n", dev->muted ? "on" : "off"); | |
313 | v4l2_info(v4l2_dev, "io = 0x%x\n", dev->io); | |
314 | v4l2_info(v4l2_dev, "mute frequency = %lu kHz\n", dev->mutefreq >> 4); | |
315 | return 0; | |
316 | } | |
1da177e4 | 317 | |
bec43661 | 318 | static const struct v4l2_file_operations typhoon_fops = { |
1da177e4 | 319 | .owner = THIS_MODULE, |
3f6892ac | 320 | .ioctl = video_ioctl2, |
1da177e4 LT |
321 | }; |
322 | ||
a399810c | 323 | static const struct v4l2_ioctl_ops typhoon_ioctl_ops = { |
f1bfe89b | 324 | .vidioc_log_status = vidioc_log_status, |
3f6892ac DL |
325 | .vidioc_querycap = vidioc_querycap, |
326 | .vidioc_g_tuner = vidioc_g_tuner, | |
327 | .vidioc_s_tuner = vidioc_s_tuner, | |
328 | .vidioc_g_audio = vidioc_g_audio, | |
329 | .vidioc_s_audio = vidioc_s_audio, | |
330 | .vidioc_g_input = vidioc_g_input, | |
331 | .vidioc_s_input = vidioc_s_input, | |
332 | .vidioc_g_frequency = vidioc_g_frequency, | |
333 | .vidioc_s_frequency = vidioc_s_frequency, | |
334 | .vidioc_queryctrl = vidioc_queryctrl, | |
335 | .vidioc_g_ctrl = vidioc_g_ctrl, | |
336 | .vidioc_s_ctrl = vidioc_s_ctrl, | |
1da177e4 LT |
337 | }; |
338 | ||
f1bfe89b | 339 | static int __init typhoon_init(void) |
0b9c2b7a | 340 | { |
f1bfe89b HV |
341 | struct typhoon *dev = &typhoon_card; |
342 | struct v4l2_device *v4l2_dev = &dev->v4l2_dev; | |
343 | int res; | |
1da177e4 | 344 | |
f1bfe89b HV |
345 | strlcpy(v4l2_dev->name, "typhoon", sizeof(v4l2_dev->name)); |
346 | dev->io = io; | |
347 | dev->curfreq = dev->mutefreq = mutefreq; | |
1da177e4 | 348 | |
f1bfe89b HV |
349 | if (dev->io == -1) { |
350 | v4l2_err(v4l2_dev, "You must set an I/O address with io=0x316 or io=0x336\n"); | |
1da177e4 LT |
351 | return -EINVAL; |
352 | } | |
1da177e4 | 353 | |
f1bfe89b HV |
354 | if (dev->mutefreq < 87000 || dev->mutefreq > 108500) { |
355 | v4l2_err(v4l2_dev, "You must set a frequency (in kHz) used when muting the card,\n"); | |
356 | v4l2_err(v4l2_dev, "e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108500)\n"); | |
1da177e4 LT |
357 | return -EINVAL; |
358 | } | |
f1bfe89b HV |
359 | |
360 | mutex_init(&dev->lock); | |
361 | if (!request_region(dev->io, 8, "typhoon")) { | |
362 | v4l2_err(v4l2_dev, "port 0x%x already in use\n", | |
363 | dev->io); | |
1da177e4 LT |
364 | return -EBUSY; |
365 | } | |
366 | ||
f1bfe89b HV |
367 | res = v4l2_device_register(NULL, v4l2_dev); |
368 | if (res < 0) { | |
369 | release_region(dev->io, 8); | |
370 | v4l2_err(v4l2_dev, "Could not register v4l2_device\n"); | |
371 | return res; | |
372 | } | |
373 | v4l2_info(v4l2_dev, BANNER); | |
374 | ||
375 | strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); | |
376 | dev->vdev.v4l2_dev = v4l2_dev; | |
377 | dev->vdev.fops = &typhoon_fops; | |
378 | dev->vdev.ioctl_ops = &typhoon_ioctl_ops; | |
379 | dev->vdev.release = video_device_release_empty; | |
380 | video_set_drvdata(&dev->vdev, dev); | |
381 | if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) { | |
382 | v4l2_device_unregister(&dev->v4l2_dev); | |
383 | release_region(dev->io, 8); | |
1da177e4 LT |
384 | return -EINVAL; |
385 | } | |
f1bfe89b HV |
386 | v4l2_info(v4l2_dev, "port 0x%x.\n", dev->io); |
387 | v4l2_info(v4l2_dev, "mute frequency is %lu kHz.\n", dev->mutefreq); | |
388 | dev->mutefreq <<= 4; | |
1da177e4 LT |
389 | |
390 | /* mute card - prevents noisy bootups */ | |
f1bfe89b | 391 | typhoon_mute(dev); |
1da177e4 LT |
392 | |
393 | return 0; | |
394 | } | |
395 | ||
f1bfe89b | 396 | static void __exit typhoon_exit(void) |
1da177e4 | 397 | { |
f1bfe89b | 398 | struct typhoon *dev = &typhoon_card; |
1da177e4 | 399 | |
f1bfe89b HV |
400 | video_unregister_device(&dev->vdev); |
401 | v4l2_device_unregister(&dev->v4l2_dev); | |
402 | release_region(dev->io, 8); | |
1da177e4 LT |
403 | } |
404 | ||
405 | module_init(typhoon_init); | |
f1bfe89b | 406 | module_exit(typhoon_exit); |
1da177e4 | 407 |