]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/radio/radio-terratec.c
V4L/DVB (8776): radio: replace video_exclusive_open/release
[mirror_ubuntu-artful-kernel.git] / drivers / media / radio / radio-terratec.c
CommitLineData
1da177e4
LT
1/* Terratec ActiveRadio ISA Standalone card driver for Linux radio support
2 * (c) 1999 R. Offermanns (rolf@offermanns.de)
3 * based on the aimslab radio driver from M. Kirkwood
4 * many thanks to Michael Becker and Friedhelm Birth (from TerraTec)
4286c6f6 5 *
1da177e4
LT
6 *
7 * History:
8 * 1999-05-21 First preview release
4286c6f6 9 *
1da177e4
LT
10 * Notes on the hardware:
11 * There are two "main" chips on the card:
12 * - Philips OM5610 (http://www-us.semiconductors.philips.com/acrobat/datasheets/OM5610_2.pdf)
13 * - Philips SAA6588 (http://www-us.semiconductors.philips.com/acrobat/datasheets/SAA6588_1.pdf)
14 * (you can get the datasheet at the above links)
15 *
16 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
17 * Volume Control is done digitally
18 *
19 * there is a I2C controlled RDS decoder (SAA6588) onboard, which i would like to support someday
20 * (as soon i have understand how to get started :)
21 * If you can help me out with that, please contact me!!
22 *
4286c6f6 23 *
55ac7b69 24 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
1da177e4
LT
25 */
26
27#include <linux/module.h> /* Modules */
28#include <linux/init.h> /* Initdata */
fb911ee8 29#include <linux/ioport.h> /* request_region */
1da177e4
LT
30#include <linux/delay.h> /* udelay */
31#include <asm/io.h> /* outb, outb_p */
32#include <asm/uaccess.h> /* copy to/from user */
55ac7b69 33#include <linux/videodev2.h> /* kernel radio structs */
5e87efa3 34#include <media/v4l2-common.h>
35ea11ff 35#include <media/v4l2-ioctl.h>
1da177e4
LT
36#include <linux/spinlock.h>
37
55ac7b69
MCC
38#include <linux/version.h> /* for KERNEL_VERSION MACRO */
39#define RADIO_VERSION KERNEL_VERSION(0,0,2)
40
41static struct v4l2_queryctrl radio_qctrl[] = {
42 {
43 .id = V4L2_CID_AUDIO_MUTE,
44 .name = "Mute",
45 .minimum = 0,
46 .maximum = 1,
47 .default_value = 1,
48 .type = V4L2_CTRL_TYPE_BOOLEAN,
49 },{
50 .id = V4L2_CID_AUDIO_VOLUME,
51 .name = "Volume",
52 .minimum = 0,
53 .maximum = 0xff,
54 .step = 1,
55 .default_value = 0xff,
56 .type = V4L2_CTRL_TYPE_INTEGER,
57 }
58};
59
1da177e4
LT
60#ifndef CONFIG_RADIO_TERRATEC_PORT
61#define CONFIG_RADIO_TERRATEC_PORT 0x590
62#endif
63
64/**************** this ones are for the terratec *******************/
65#define BASEPORT 0x590
66#define VOLPORT 0x591
67#define WRT_DIS 0x00
68#define CLK_OFF 0x00
69#define IIC_DATA 0x01
70#define IIC_CLK 0x02
71#define DATA 0x04
72#define CLK_ON 0x08
73#define WRT_EN 0x10
74/*******************************************************************/
75
4286c6f6 76static int io = CONFIG_RADIO_TERRATEC_PORT;
1da177e4
LT
77static int radio_nr = -1;
78static spinlock_t lock;
79
80struct tt_device
81{
3ca685aa 82 unsigned long in_use;
1da177e4
LT
83 int port;
84 int curvol;
85 unsigned long curfreq;
86 int muted;
87};
88
89
90/* local things */
91
92static void cardWriteVol(int volume)
93{
94 int i;
95 volume = volume+(volume * 32); // change both channels
96 spin_lock(&lock);
97 for (i=0;i<8;i++)
98 {
99 if (volume & (0x80>>i))
100 outb(0x80, VOLPORT);
101 else outb(0x00, VOLPORT);
102 }
103 spin_unlock(&lock);
104}
105
106
107
108static void tt_mute(struct tt_device *dev)
109{
110 dev->muted = 1;
111 cardWriteVol(0);
112}
113
114static int tt_setvol(struct tt_device *dev, int vol)
115{
4286c6f6 116
1da177e4
LT
117// printk(KERN_ERR "setvol called, vol = %d\n", vol);
118
119 if(vol == dev->curvol) { /* requested volume = current */
120 if (dev->muted) { /* user is unmuting the card */
121 dev->muted = 0;
122 cardWriteVol(vol); /* enable card */
4286c6f6
MCC
123 }
124
1da177e4
LT
125 return 0;
126 }
127
128 if(vol == 0) { /* volume = 0 means mute the card */
129 cardWriteVol(0); /* "turn off card" by setting vol to 0 */
130 dev->curvol = vol; /* track the volume state! */
131 return 0;
132 }
133
134 dev->muted = 0;
4286c6f6 135
1da177e4 136 cardWriteVol(vol);
4286c6f6 137
1da177e4
LT
138 dev->curvol = vol;
139
140 return 0;
141
142}
143
144
145/* this is the worst part in this driver */
146/* many more or less strange things are going on here, but hey, it works :) */
147
148static int tt_setfreq(struct tt_device *dev, unsigned long freq1)
4286c6f6 149{
1da177e4
LT
150 int freq;
151 int i;
152 int p;
153 int temp;
154 long rest;
4286c6f6 155
1da177e4
LT
156 unsigned char buffer[25]; /* we have to bit shift 25 registers */
157 freq = freq1/160; /* convert the freq. to a nice to handle value */
158 for(i=24;i>-1;i--)
159 buffer[i]=0;
160
161 rest = freq*10+10700; /* i once had understood what is going on here */
162 /* maybe some wise guy (friedhelm?) can comment this stuff */
163 i=13;
164 p=10;
165 temp=102400;
166 while (rest!=0)
167 {
168 if (rest%temp == rest)
169 buffer[i] = 0;
4286c6f6 170 else
1da177e4 171 {
4286c6f6 172 buffer[i] = 1;
1da177e4
LT
173 rest = rest-temp;
174 }
175 i--;
176 p--;
177 temp = temp/2;
b930e1d8 178 }
1da177e4
LT
179
180 spin_lock(&lock);
4286c6f6 181
1da177e4
LT
182 for (i=24;i>-1;i--) /* bit shift the values to the radiocard */
183 {
4286c6f6 184 if (buffer[i]==1)
1da177e4
LT
185 {
186 outb(WRT_EN|DATA, BASEPORT);
187 outb(WRT_EN|DATA|CLK_ON , BASEPORT);
188 outb(WRT_EN|DATA, BASEPORT);
189 }
190 else
191 {
192 outb(WRT_EN|0x00, BASEPORT);
193 outb(WRT_EN|0x00|CLK_ON , BASEPORT);
194 }
195 }
4286c6f6
MCC
196 outb(0x00, BASEPORT);
197
1da177e4 198 spin_unlock(&lock);
4286c6f6
MCC
199
200 return 0;
1da177e4
LT
201}
202
203static int tt_getsigstr(struct tt_device *dev) /* TODO */
204{
205 if (inb(io) & 2) /* bit set = no signal present */
206 return 0;
207 return 1; /* signal present */
208}
209
1de69238
DL
210static int vidioc_querycap(struct file *file, void *priv,
211 struct v4l2_capability *v)
212{
213 strlcpy(v->driver, "radio-terratec", sizeof(v->driver));
214 strlcpy(v->card, "ActiveRadio", sizeof(v->card));
215 sprintf(v->bus_info, "ISA");
216 v->version = RADIO_VERSION;
217 v->capabilities = V4L2_CAP_TUNER;
218 return 0;
219}
1da177e4 220
1de69238
DL
221static int vidioc_g_tuner(struct file *file, void *priv,
222 struct v4l2_tuner *v)
1da177e4
LT
223{
224 struct video_device *dev = video_devdata(file);
1de69238 225 struct tt_device *tt = dev->priv;
4286c6f6 226
1de69238
DL
227 if (v->index > 0)
228 return -EINVAL;
55ac7b69 229
1de69238
DL
230 strcpy(v->name, "FM");
231 v->type = V4L2_TUNER_RADIO;
232 v->rangelow = (87*16000);
233 v->rangehigh = (108*16000);
234 v->rxsubchans = V4L2_TUNER_SUB_MONO;
235 v->capability = V4L2_TUNER_CAP_LOW;
236 v->audmode = V4L2_TUNER_MODE_MONO;
237 v->signal = 0xFFFF*tt_getsigstr(tt);
238 return 0;
239}
55ac7b69 240
1de69238
DL
241static int vidioc_s_tuner(struct file *file, void *priv,
242 struct v4l2_tuner *v)
243{
244 if (v->index > 0)
245 return -EINVAL;
246 return 0;
247}
55ac7b69 248
1de69238
DL
249static int vidioc_s_frequency(struct file *file, void *priv,
250 struct v4l2_frequency *f)
251{
252 struct video_device *dev = video_devdata(file);
253 struct tt_device *tt = dev->priv;
55ac7b69 254
1de69238
DL
255 tt->curfreq = f->frequency;
256 tt_setfreq(tt, tt->curfreq);
257 return 0;
258}
55ac7b69 259
1de69238
DL
260static int vidioc_g_frequency(struct file *file, void *priv,
261 struct v4l2_frequency *f)
262{
263 struct video_device *dev = video_devdata(file);
264 struct tt_device *tt = dev->priv;
55ac7b69 265
1de69238
DL
266 f->type = V4L2_TUNER_RADIO;
267 f->frequency = tt->curfreq;
268 return 0;
269}
55ac7b69 270
1de69238
DL
271static int vidioc_queryctrl(struct file *file, void *priv,
272 struct v4l2_queryctrl *qc)
273{
274 int i;
55ac7b69 275
1de69238
DL
276 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
277 if (qc->id && qc->id == radio_qctrl[i].id) {
278 memcpy(qc, &(radio_qctrl[i]),
279 sizeof(*qc));
1da177e4
LT
280 return 0;
281 }
1de69238
DL
282 }
283 return -EINVAL;
284}
55ac7b69 285
1de69238
DL
286static int vidioc_g_ctrl(struct file *file, void *priv,
287 struct v4l2_control *ctrl)
288{
289 struct video_device *dev = video_devdata(file);
290 struct tt_device *tt = dev->priv;
55ac7b69 291
1de69238
DL
292 switch (ctrl->id) {
293 case V4L2_CID_AUDIO_MUTE:
294 if (tt->muted)
295 ctrl->value = 1;
296 else
297 ctrl->value = 0;
298 return 0;
299 case V4L2_CID_AUDIO_VOLUME:
300 ctrl->value = tt->curvol * 6554;
301 return 0;
302 }
303 return -EINVAL;
304}
55ac7b69 305
1de69238
DL
306static int vidioc_s_ctrl(struct file *file, void *priv,
307 struct v4l2_control *ctrl)
308{
309 struct video_device *dev = video_devdata(file);
310 struct tt_device *tt = dev->priv;
311
312 switch (ctrl->id) {
313 case V4L2_CID_AUDIO_MUTE:
314 if (ctrl->value)
315 tt_mute(tt);
316 else
317 tt_setvol(tt,tt->curvol);
318 return 0;
319 case V4L2_CID_AUDIO_VOLUME:
320 tt_setvol(tt,ctrl->value);
321 return 0;
1da177e4 322 }
1de69238
DL
323 return -EINVAL;
324}
325
326static int vidioc_g_audio(struct file *file, void *priv,
327 struct v4l2_audio *a)
328{
329 if (a->index > 1)
330 return -EINVAL;
331
332 strcpy(a->name, "Radio");
333 a->capability = V4L2_AUDCAP_STEREO;
334 return 0;
335}
336
337static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
338{
339 *i = 0;
340 return 0;
1da177e4
LT
341}
342
1de69238 343static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1da177e4 344{
1de69238
DL
345 if (i != 0)
346 return -EINVAL;
347 return 0;
348}
349
350static int vidioc_s_audio(struct file *file, void *priv,
351 struct v4l2_audio *a)
352{
353 if (a->index != 0)
354 return -EINVAL;
355 return 0;
1da177e4
LT
356}
357
358static struct tt_device terratec_unit;
359
3ca685aa
HV
360static int terratec_exclusive_open(struct inode *inode, struct file *file)
361{
362 return test_and_set_bit(0, &terratec_unit.in_use) ? -EBUSY : 0;
363}
364
365static int terratec_exclusive_release(struct inode *inode, struct file *file)
366{
367 clear_bit(0, &terratec_unit.in_use);
368 return 0;
369}
370
fa027c2a 371static const struct file_operations terratec_fops = {
1da177e4 372 .owner = THIS_MODULE,
3ca685aa
HV
373 .open = terratec_exclusive_open,
374 .release = terratec_exclusive_release,
1de69238 375 .ioctl = video_ioctl2,
078ff795 376#ifdef CONFIG_COMPAT
0d0fbf81 377 .compat_ioctl = v4l_compat_ioctl32,
078ff795 378#endif
1da177e4
LT
379 .llseek = no_llseek,
380};
381
a399810c 382static const struct v4l2_ioctl_ops terratec_ioctl_ops = {
1de69238
DL
383 .vidioc_querycap = vidioc_querycap,
384 .vidioc_g_tuner = vidioc_g_tuner,
385 .vidioc_s_tuner = vidioc_s_tuner,
386 .vidioc_g_frequency = vidioc_g_frequency,
387 .vidioc_s_frequency = vidioc_s_frequency,
388 .vidioc_queryctrl = vidioc_queryctrl,
389 .vidioc_g_ctrl = vidioc_g_ctrl,
390 .vidioc_s_ctrl = vidioc_s_ctrl,
391 .vidioc_g_audio = vidioc_g_audio,
392 .vidioc_s_audio = vidioc_s_audio,
393 .vidioc_g_input = vidioc_g_input,
394 .vidioc_s_input = vidioc_s_input,
1da177e4
LT
395};
396
a399810c 397static struct video_device terratec_radio = {
a399810c 398 .name = "TerraTec ActiveRadio",
a399810c
HV
399 .fops = &terratec_fops,
400 .ioctl_ops = &terratec_ioctl_ops,
401};
402
1da177e4
LT
403static int __init terratec_init(void)
404{
405 if(io==-1)
406 {
407 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
408 return -EINVAL;
409 }
4286c6f6 410 if (!request_region(io, 2, "terratec"))
1da177e4
LT
411 {
412 printk(KERN_ERR "TerraTec: port 0x%x already in use\n", io);
413 return -EBUSY;
414 }
415
416 terratec_radio.priv=&terratec_unit;
4286c6f6 417
1da177e4 418 spin_lock_init(&lock);
4286c6f6 419
cba99ae8 420 if (video_register_device(&terratec_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
1da177e4
LT
421 release_region(io,2);
422 return -EINVAL;
423 }
4286c6f6 424
1da177e4
LT
425 printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver.\n");
426
4286c6f6 427 /* mute card - prevents noisy bootups */
1da177e4
LT
428
429 /* this ensures that the volume is all the way down */
430 cardWriteVol(0);
431 terratec_unit.curvol = 0;
432
433 return 0;
434}
435
436MODULE_AUTHOR("R.OFFERMANNS & others");
437MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
438MODULE_LICENSE("GPL");
439module_param(io, int, 0);
440MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
441module_param(radio_nr, int, 0);
442
443static void __exit terratec_cleanup_module(void)
444{
445 video_unregister_device(&terratec_radio);
446 release_region(io,2);
4286c6f6 447 printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver unloaded.\n");
1da177e4
LT
448}
449
450module_init(terratec_init);
451module_exit(terratec_cleanup_module);
452