Import 2.2.8pre2
[davej-history.git] / drivers / char / radio-rtrack2.c
blob793548839803a84a879e7d4667641938c4293038
1 /* RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
2 *
3 * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
4 * Coverted to new API by Alan Cox <Alan.Cox@linux.org>
5 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
7 * TODO: Allow for more than one of these foolish entities :-)
9 */
11 #include <linux/module.h> /* Modules */
12 #include <linux/init.h> /* Initdata */
13 #include <linux/ioport.h> /* check_region, request_region */
14 #include <linux/delay.h> /* udelay */
15 #include <asm/io.h> /* outb, outb_p */
16 #include <asm/uaccess.h> /* copy to/from user */
17 #include <linux/videodev.h> /* kernel radio structs */
18 #include <linux/config.h> /* CONFIG_RADIO_RTRACK2_PORT */
20 #ifndef CONFIG_RADIO_RTRACK2_PORT
21 #define CONFIG_RADIO_RTRACK2_PORT -1
22 #endif
24 static int io = CONFIG_RADIO_RTRACK2_PORT;
25 static int users = 0;
27 struct rt_device
29 int port;
30 unsigned long curfreq;
31 int muted;
35 /* local things */
37 static void rt_mute(struct rt_device *dev)
39 if(dev->muted)
40 return;
41 outb(1, io);
42 dev->muted = 1;
45 static void rt_unmute(struct rt_device *dev)
47 if(dev->muted == 0)
48 return;
49 outb(0, io);
50 dev->muted = 0;
53 static void zero(void)
55 outb_p(1, io);
56 outb_p(3, io);
57 outb_p(1, io);
60 static void one(void)
62 outb_p(5, io);
63 outb_p(7, io);
64 outb_p(5, io);
67 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
69 int i;
71 freq = freq / 200 + 856;
73 outb_p(0xc8, io);
74 outb_p(0xc9, io);
75 outb_p(0xc9, io);
77 for (i = 0; i < 10; i++)
78 zero ();
80 for (i = 14; i >= 0; i--)
81 if (freq & (1 << i))
82 one ();
83 else
84 zero ();
86 outb_p(0xc8, io);
87 if (!dev->muted)
88 outb_p(0, io);
89 return 0;
92 int rt_getsigstr(struct rt_device *dev)
94 if (inb(io) & 2) /* bit set = no signal present */
95 return 0;
96 return 1; /* signal present */
99 static int rt_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
101 struct rt_device *rt=dev->priv;
103 switch(cmd)
105 case VIDIOCGCAP:
107 struct video_capability v;
108 v.type=VID_TYPE_TUNER;
109 v.channels=1;
110 v.audios=1;
111 /* No we don't do pictures */
112 v.maxwidth=0;
113 v.maxheight=0;
114 v.minwidth=0;
115 v.minheight=0;
116 strcpy(v.name, "RadioTrack II");
117 if(copy_to_user(arg,&v,sizeof(v)))
118 return -EFAULT;
119 return 0;
121 case VIDIOCGTUNER:
123 struct video_tuner v;
124 if(copy_from_user(&v, arg,sizeof(v))!=0)
125 return -EFAULT;
126 if(v.tuner) /* Only 1 tuner */
127 return -EINVAL;
128 v.rangelow=88*16000;
129 v.rangehigh=108*16000;
130 v.flags=VIDEO_TUNER_LOW;
131 v.mode=VIDEO_MODE_AUTO;
132 v.signal=0xFFFF*rt_getsigstr(rt);
133 strcpy(v.name, "FM");
134 if(copy_to_user(arg,&v, sizeof(v)))
135 return -EFAULT;
136 return 0;
138 case VIDIOCSTUNER:
140 struct video_tuner v;
141 if(copy_from_user(&v, arg, sizeof(v)))
142 return -EFAULT;
143 if(v.tuner!=0)
144 return -EINVAL;
145 /* Only 1 tuner so no setting needed ! */
146 return 0;
148 case VIDIOCGFREQ:
149 if(copy_to_user(arg, &rt->curfreq, sizeof(rt->curfreq)))
150 return -EFAULT;
151 return 0;
152 case VIDIOCSFREQ:
153 if(copy_from_user(&rt->curfreq, arg,sizeof(rt->curfreq)))
154 return -EFAULT;
155 rt_setfreq(rt, rt->curfreq);
156 return 0;
157 case VIDIOCGAUDIO:
159 struct video_audio v;
160 memset(&v,0, sizeof(v));
161 v.flags|=VIDEO_AUDIO_MUTABLE;
162 v.volume=1;
163 v.step=65535;
164 strcpy(v.name, "Radio");
165 if(copy_to_user(arg,&v, sizeof(v)))
166 return -EFAULT;
167 return 0;
169 case VIDIOCSAUDIO:
171 struct video_audio v;
172 if(copy_from_user(&v, arg, sizeof(v)))
173 return -EFAULT;
174 if(v.audio)
175 return -EINVAL;
177 if(v.flags&VIDEO_AUDIO_MUTE)
178 rt_mute(rt);
179 else
180 rt_unmute(rt);
182 return 0;
184 default:
185 return -ENOIOCTLCMD;
189 static int rt_open(struct video_device *dev, int flags)
191 if(users)
192 return -EBUSY;
193 users++;
194 MOD_INC_USE_COUNT;
195 return 0;
198 static void rt_close(struct video_device *dev)
200 users--;
201 MOD_DEC_USE_COUNT;
204 static struct rt_device rtrack2_unit;
206 static struct video_device rtrack2_radio=
208 "RadioTrack II radio",
209 VID_TYPE_TUNER,
210 VID_HARDWARE_RTRACK2,
211 rt_open,
212 rt_close,
213 NULL, /* Can't read (no capture ability) */
214 NULL, /* Can't write */
215 NULL, /* Can't poll */
216 rt_ioctl,
217 NULL,
218 NULL
221 __initfunc(int rtrack2_init(struct video_init *v))
223 if (check_region(io, 4))
225 printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
226 return -EBUSY;
229 rtrack2_radio.priv=&rtrack2_unit;
231 if(video_register_device(&rtrack2_radio, VFL_TYPE_RADIO)==-1)
232 return -EINVAL;
234 request_region(io, 4, "rtrack2");
235 printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
237 /* mute card - prevents noisy bootups */
238 outb(1, io);
239 rtrack2_unit.muted = 1;
241 return 0;
244 #ifdef MODULE
246 MODULE_AUTHOR("Ben Pfaff");
247 MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
248 MODULE_PARM(io, "i");
249 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
251 EXPORT_NO_SYMBOLS;
253 int init_module(void)
255 if(io==-1)
257 printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
258 return -EINVAL;
260 return rtrack2_init(NULL);
263 void cleanup_module(void)
265 video_unregister_device(&rtrack2_radio);
266 release_region(io,4);
269 #endif
272 Local variables:
273 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"
274 End: