Import 2.1.118
[davej-history.git] / drivers / char / radio-rtrack2.c
blob175c665f7bba7f983b8f7e6c6037002ef687b2b5
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 if(copy_to_user(arg,&v, sizeof(v)))
134 return -EFAULT;
135 return 0;
137 case VIDIOCSTUNER:
139 struct video_tuner v;
140 if(copy_from_user(&v, arg, sizeof(v)))
141 return -EFAULT;
142 if(v.tuner!=0)
143 return -EINVAL;
144 /* Only 1 tuner so no setting needed ! */
145 return 0;
147 case VIDIOCGFREQ:
148 if(copy_to_user(arg, &rt->curfreq, sizeof(rt->curfreq)))
149 return -EFAULT;
150 return 0;
151 case VIDIOCSFREQ:
152 if(copy_from_user(&rt->curfreq, arg,sizeof(rt->curfreq)))
153 return -EFAULT;
154 rt_setfreq(rt, rt->curfreq);
155 return 0;
156 case VIDIOCGAUDIO:
158 struct video_audio v;
159 memset(&v,0, sizeof(v));
160 v.flags|=VIDEO_AUDIO_MUTABLE;
161 v.volume=1;
162 v.step=65535;
163 strcpy(v.name, "Radio");
164 if(copy_to_user(arg,&v, sizeof(v)))
165 return -EFAULT;
166 return 0;
168 case VIDIOCSAUDIO:
170 struct video_audio v;
171 if(copy_from_user(&v, arg, sizeof(v)))
172 return -EFAULT;
173 if(v.audio)
174 return -EINVAL;
176 if(v.flags&VIDEO_AUDIO_MUTE)
177 rt_mute(rt);
178 else
179 rt_unmute(rt);
181 return 0;
183 default:
184 return -ENOIOCTLCMD;
188 static int rt_open(struct video_device *dev, int flags)
190 if(users)
191 return -EBUSY;
192 users++;
193 MOD_INC_USE_COUNT;
194 return 0;
197 static void rt_close(struct video_device *dev)
199 users--;
200 MOD_DEC_USE_COUNT;
203 static struct rt_device rtrack2_unit;
205 static struct video_device rtrack2_radio=
207 "RadioTrack II radio",
208 VID_TYPE_TUNER,
209 VID_HARDWARE_RTRACK2,
210 rt_open,
211 rt_close,
212 NULL, /* Can't read (no capture ability) */
213 NULL, /* Can't write */
214 NULL, /* Can't poll */
215 rt_ioctl,
216 NULL,
217 NULL
220 __initfunc(int rtrack2_init(struct video_init *v))
222 if (check_region(io, 4))
224 printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
225 return -EBUSY;
228 rtrack2_radio.priv=&rtrack2_unit;
230 if(video_register_device(&rtrack2_radio, VFL_TYPE_RADIO)==-1)
231 return -EINVAL;
233 request_region(io, 4, "rtrack2");
234 printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
236 /* mute card - prevents noisy bootups */
237 outb(1, io);
238 rtrack2_unit.muted = 1;
240 return 0;
243 #ifdef MODULE
245 MODULE_AUTHOR("Ben Pfaff");
246 MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
247 MODULE_PARM(io, "i");
248 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
250 EXPORT_NO_SYMBOLS;
252 int init_module(void)
254 if(io==-1)
256 printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
257 return -EINVAL;
259 return rtrack2_init(NULL);
262 void cleanup_module(void)
264 video_unregister_device(&rtrack2_radio);
265 release_region(io,4);
268 #endif
271 Local variables:
272 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"
273 End: