Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / radio-gemtek.c
blob8b53dbd0df5dc4b9c3b6e88f436a1386576d57b1
1 /* GemTek radio card driver for Linux (C) 1998 Jonas Munsin <jmunsin@iki.fi>
3 * GemTek hasn't released any specs on the card, so the protocol had to
4 * be reverse engineered with dosemu.
6 * Besides the protocol changes, this is mostly a copy of:
8 * RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
9 *
10 * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
11 * Coverted to new API by Alan Cox <Alan.Cox@linux.org>
12 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
14 * TODO: Allow for more than one of these foolish entities :-)
18 #include <linux/module.h> /* Modules */
19 #include <linux/init.h> /* Initdata */
20 #include <linux/ioport.h> /* check_region, request_region */
21 #include <linux/delay.h> /* udelay */
22 #include <asm/io.h> /* outb, outb_p */
23 #include <asm/uaccess.h> /* copy to/from user */
24 #include <linux/videodev.h> /* kernel radio structs */
25 #include <linux/config.h> /* CONFIG_RADIO_GEMTEK_PORT */
26 #include <linux/spinlock.h>
28 #ifndef CONFIG_RADIO_GEMTEK_PORT
29 #define CONFIG_RADIO_GEMTEK_PORT -1
30 #endif
32 static int io = CONFIG_RADIO_GEMTEK_PORT;
33 static int users = 0;
34 static spinlock_t lock;
36 struct gemtek_device
38 int port;
39 unsigned long curfreq;
40 int muted;
44 /* local things */
46 /* the correct way to mute the gemtek may be to write the last written
47 * frequency || 0x10, but just writing 0x10 once seems to do it as well
49 static void gemtek_mute(struct gemtek_device *dev)
51 if(dev->muted)
52 return;
53 spin_lock(&lock);
54 outb(0x10, io);
55 spin_unlock(&lock);
56 dev->muted = 1;
59 static void gemtek_unmute(struct gemtek_device *dev)
61 if(dev->muted == 0)
62 return;
63 spin_lock(&lock);
64 outb(0x20, io);
65 spin_unlock(&lock);
66 dev->muted = 0;
69 static void zero(void)
71 outb_p(0x04, io);
72 udelay(5);
73 outb_p(0x05, io);
74 udelay(5);
77 static void one(void)
79 outb_p(0x06, io);
80 udelay(5);
81 outb_p(0x07, io);
82 udelay(5);
85 static int gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
87 int i;
89 /* freq = 78.25*((float)freq/16000.0 + 10.52); */
91 freq /= 16;
92 freq += 10520;
93 freq *= 7825;
94 freq /= 100000;
96 spin_lock(&lock);
98 /* 2 start bits */
99 outb_p(0x03, io);
100 udelay(5);
101 outb_p(0x07, io);
102 udelay(5);
104 /* 28 frequency bits (lsb first) */
105 for (i = 0; i < 14; i++)
106 if (freq & (1 << i))
107 one();
108 else
109 zero();
110 /* 36 unknown bits */
111 for (i = 0; i < 11; i++)
112 zero();
113 one();
114 for (i = 0; i < 4; i++)
115 zero();
116 one();
117 zero();
119 /* 2 end bits */
120 outb_p(0x03, io);
121 udelay(5);
122 outb_p(0x07, io);
123 udelay(5);
125 spin_unlock(&lock);
127 return 0;
130 int gemtek_getsigstr(struct gemtek_device *dev)
132 spin_lock(&lock);
133 inb(io);
134 udelay(5);
135 spin_unlock(&lock);
136 if (inb(io) & 8) /* bit set = no signal present */
137 return 0;
138 return 1; /* signal present */
141 static int gemtek_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
143 struct gemtek_device *rt=dev->priv;
145 switch(cmd)
147 case VIDIOCGCAP:
149 struct video_capability v;
150 v.type=VID_TYPE_TUNER;
151 v.channels=1;
152 v.audios=1;
153 /* No we don't do pictures */
154 v.maxwidth=0;
155 v.maxheight=0;
156 v.minwidth=0;
157 v.minheight=0;
158 strcpy(v.name, "GemTek");
159 if(copy_to_user(arg,&v,sizeof(v)))
160 return -EFAULT;
161 return 0;
163 case VIDIOCGTUNER:
165 struct video_tuner v;
166 if(copy_from_user(&v, arg,sizeof(v))!=0)
167 return -EFAULT;
168 if(v.tuner) /* Only 1 tuner */
169 return -EINVAL;
170 v.rangelow=87*16000;
171 v.rangehigh=108*16000;
172 v.flags=VIDEO_TUNER_LOW;
173 v.mode=VIDEO_MODE_AUTO;
174 v.signal=0xFFFF*gemtek_getsigstr(rt);
175 strcpy(v.name, "FM");
176 if(copy_to_user(arg,&v, sizeof(v)))
177 return -EFAULT;
178 return 0;
180 case VIDIOCSTUNER:
182 struct video_tuner v;
183 if(copy_from_user(&v, arg, sizeof(v)))
184 return -EFAULT;
185 if(v.tuner!=0)
186 return -EINVAL;
187 /* Only 1 tuner so no setting needed ! */
188 return 0;
190 case VIDIOCGFREQ:
191 if(copy_to_user(arg, &rt->curfreq, sizeof(rt->curfreq)))
192 return -EFAULT;
193 return 0;
194 case VIDIOCSFREQ:
195 if(copy_from_user(&rt->curfreq, arg,sizeof(rt->curfreq)))
196 return -EFAULT;
197 /* needs to be called twice in order for getsigstr to work */
198 gemtek_setfreq(rt, rt->curfreq);
199 gemtek_setfreq(rt, rt->curfreq);
200 return 0;
201 case VIDIOCGAUDIO:
203 struct video_audio v;
204 memset(&v,0, sizeof(v));
205 v.flags|=VIDEO_AUDIO_MUTABLE;
206 v.volume=1;
207 v.step=65535;
208 strcpy(v.name, "Radio");
209 if(copy_to_user(arg,&v, sizeof(v)))
210 return -EFAULT;
211 return 0;
213 case VIDIOCSAUDIO:
215 struct video_audio v;
216 if(copy_from_user(&v, arg, sizeof(v)))
217 return -EFAULT;
218 if(v.audio)
219 return -EINVAL;
221 if(v.flags&VIDEO_AUDIO_MUTE)
222 gemtek_mute(rt);
223 else
224 gemtek_unmute(rt);
226 return 0;
228 default:
229 return -ENOIOCTLCMD;
233 static int gemtek_open(struct video_device *dev, int flags)
235 if(users)
236 return -EBUSY;
237 users++;
238 MOD_INC_USE_COUNT;
239 return 0;
242 static void gemtek_close(struct video_device *dev)
244 users--;
245 MOD_DEC_USE_COUNT;
248 static struct gemtek_device gemtek_unit;
250 static struct video_device gemtek_radio=
252 "GemTek radio",
253 VID_TYPE_TUNER,
254 VID_HARDWARE_GEMTEK,
255 gemtek_open,
256 gemtek_close,
257 NULL, /* Can't read (no capture ability) */
258 NULL, /* Can't write */
259 NULL, /* Can't poll */
260 gemtek_ioctl,
261 NULL,
262 NULL
265 static int __init gemtek_init(void)
267 if(io==-1)
269 printk(KERN_ERR "You must set an I/O address with io=0x20c, io=0x30c, io=0x24c or io=0x34c (io=0x020c or io=0x248 for the combined sound/radiocard)\n");
270 return -EINVAL;
273 if (check_region(io, 4))
275 printk(KERN_ERR "gemtek: port 0x%x already in use\n", io);
276 return -EBUSY;
279 gemtek_radio.priv=&gemtek_unit;
281 if(video_register_device(&gemtek_radio, VFL_TYPE_RADIO)==-1)
282 return -EINVAL;
284 request_region(io, 4, "gemtek");
285 printk(KERN_INFO "GemTek Radio Card driver.\n");
287 spin_lock_init(&lock);
288 /* mute card - prevents noisy bootups */
289 outb(0x10, io);
290 udelay(5);
291 gemtek_unit.muted = 1;
293 /* this is _maybe_ unnecessary */
294 outb(0x01, io);
296 return 0;
299 MODULE_AUTHOR("Jonas Munsin");
300 MODULE_DESCRIPTION("A driver for the GemTek Radio Card");
301 MODULE_PARM(io, "i");
302 MODULE_PARM_DESC(io, "I/O address of the GemTek card (0x20c, 0x30c, 0x24c or 0x34c (0x20c or 0x248 have been reported to work for the combined sound/radiocard)).");
304 EXPORT_NO_SYMBOLS;
306 static void __exit gemtek_cleanup(void)
308 video_unregister_device(&gemtek_radio);
309 release_region(io,4);
312 module_init(gemtek_init);
313 module_exit(gemtek_cleanup);
316 Local variables:
317 compile-command: "gcc -c -DMODVERSIONS -D__KERNEL__ -DMODULE -O6 -Wall -Wstrict-prototypes -I /home/blp/tmp/linux-2.1.111-rtrack/include radio-rtrack2.c"
318 End: