V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / media / video / tvmixer.c
blob9fa5b702e0736ddfc99b26177bae0c547aefd70d
1 /*
2 */
4 #include <linux/module.h>
5 #include <linux/kernel.h>
6 #include <linux/string.h>
7 #include <linux/timer.h>
8 #include <linux/delay.h>
9 #include <linux/errno.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/videodev.h>
13 #include <linux/init.h>
14 #include <linux/kdev_t.h>
15 #include <linux/sound.h>
16 #include <linux/soundcard.h>
18 #include <asm/semaphore.h>
19 #include <asm/uaccess.h>
22 #define DEV_MAX 4
24 static int devnr = -1;
25 module_param(devnr, int, 0644);
27 MODULE_AUTHOR("Gerd Knorr");
28 MODULE_LICENSE("GPL");
30 /* ----------------------------------------------------------------------- */
32 struct TVMIXER {
33 struct i2c_client *dev;
34 int minor;
35 int count;
38 static struct TVMIXER devices[DEV_MAX];
40 static int tvmixer_adapters(struct i2c_adapter *adap);
41 static int tvmixer_clients(struct i2c_client *client);
43 /* ----------------------------------------------------------------------- */
45 static int mix_to_v4l(int i)
47 int r;
49 r = ((i & 0xff) * 65536 + 50) / 100;
50 if (r > 65535) r = 65535;
51 if (r < 0) r = 0;
52 return r;
55 static int v4l_to_mix(int i)
57 int r;
59 r = (i * 100 + 32768) / 65536;
60 if (r > 100) r = 100;
61 if (r < 0) r = 0;
62 return r | (r << 8);
65 static int v4l_to_mix2(int l, int r)
67 r = (r * 100 + 32768) / 65536;
68 if (r > 100) r = 100;
69 if (r < 0) r = 0;
70 l = (l * 100 + 32768) / 65536;
71 if (l > 100) l = 100;
72 if (l < 0) l = 0;
73 return (r << 8) | l;
76 static int tvmixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
78 struct video_audio va;
79 int left,right,ret,val = 0;
80 struct TVMIXER *mix = file->private_data;
81 struct i2c_client *client = mix->dev;
82 void __user *argp = (void __user *)arg;
83 int __user *p = argp;
85 if (NULL == client)
86 return -ENODEV;
88 if (cmd == SOUND_MIXER_INFO) {
89 mixer_info info;
90 strlcpy(info.id, "tv card", sizeof(info.id));
91 strlcpy(info.name, client->name, sizeof(info.name));
92 info.modify_counter = 42 /* FIXME */;
93 if (copy_to_user(argp, &info, sizeof(info)))
94 return -EFAULT;
95 return 0;
97 if (cmd == SOUND_OLD_MIXER_INFO) {
98 _old_mixer_info info;
99 strlcpy(info.id, "tv card", sizeof(info.id));
100 strlcpy(info.name, client->name, sizeof(info.name));
101 if (copy_to_user(argp, &info, sizeof(info)))
102 return -EFAULT;
103 return 0;
105 if (cmd == OSS_GETVERSION)
106 return put_user(SOUND_VERSION, p);
108 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
109 if (get_user(val, p))
110 return -EFAULT;
112 /* read state */
113 memset(&va,0,sizeof(va));
114 client->driver->command(client,VIDIOCGAUDIO,&va);
116 switch (cmd) {
117 case MIXER_READ(SOUND_MIXER_RECMASK):
118 case MIXER_READ(SOUND_MIXER_CAPS):
119 case MIXER_READ(SOUND_MIXER_RECSRC):
120 case MIXER_WRITE(SOUND_MIXER_RECSRC):
121 ret = 0;
122 break;
124 case MIXER_READ(SOUND_MIXER_STEREODEVS):
125 ret = SOUND_MASK_VOLUME;
126 break;
127 case MIXER_READ(SOUND_MIXER_DEVMASK):
128 ret = SOUND_MASK_VOLUME;
129 if (va.flags & VIDEO_AUDIO_BASS)
130 ret |= SOUND_MASK_BASS;
131 if (va.flags & VIDEO_AUDIO_TREBLE)
132 ret |= SOUND_MASK_TREBLE;
133 break;
135 case MIXER_WRITE(SOUND_MIXER_VOLUME):
136 left = mix_to_v4l(val);
137 right = mix_to_v4l(val >> 8);
138 va.volume = max(left,right);
139 va.balance = (32768*min(left,right)) / (va.volume ? va.volume : 1);
140 va.balance = (left<right) ? (65535-va.balance) : va.balance;
141 if (va.volume)
142 va.flags &= ~VIDEO_AUDIO_MUTE;
143 client->driver->command(client,VIDIOCSAUDIO,&va);
144 client->driver->command(client,VIDIOCGAUDIO,&va);
145 /* fall throuth */
146 case MIXER_READ(SOUND_MIXER_VOLUME):
147 left = (min(65536 - va.balance,32768) *
148 va.volume) / 32768;
149 right = (min(va.balance,(u16)32768) *
150 va.volume) / 32768;
151 ret = v4l_to_mix2(left,right);
152 break;
154 case MIXER_WRITE(SOUND_MIXER_BASS):
155 va.bass = mix_to_v4l(val);
156 client->driver->command(client,VIDIOCSAUDIO,&va);
157 client->driver->command(client,VIDIOCGAUDIO,&va);
158 /* fall throuth */
159 case MIXER_READ(SOUND_MIXER_BASS):
160 ret = v4l_to_mix(va.bass);
161 break;
163 case MIXER_WRITE(SOUND_MIXER_TREBLE):
164 va.treble = mix_to_v4l(val);
165 client->driver->command(client,VIDIOCSAUDIO,&va);
166 client->driver->command(client,VIDIOCGAUDIO,&va);
167 /* fall throuth */
168 case MIXER_READ(SOUND_MIXER_TREBLE):
169 ret = v4l_to_mix(va.treble);
170 break;
172 default:
173 return -EINVAL;
175 if (put_user(ret, p))
176 return -EFAULT;
177 return 0;
180 static int tvmixer_open(struct inode *inode, struct file *file)
182 int i, minor = iminor(inode);
183 struct TVMIXER *mix = NULL;
184 struct i2c_client *client = NULL;
186 for (i = 0; i < DEV_MAX; i++) {
187 if (devices[i].minor == minor) {
188 mix = devices+i;
189 client = mix->dev;
190 break;
194 if (NULL == client)
195 return -ENODEV;
197 /* lock bttv in memory while the mixer is in use */
198 file->private_data = mix;
199 if (client->adapter->owner)
200 try_module_get(client->adapter->owner);
201 return 0;
204 static int tvmixer_release(struct inode *inode, struct file *file)
206 struct TVMIXER *mix = file->private_data;
207 struct i2c_client *client;
209 client = mix->dev;
210 if (NULL == client) {
211 return -ENODEV;
214 module_put(client->adapter->owner);
215 return 0;
218 static struct i2c_driver driver = {
219 .driver = {
220 .name = "tvmixer",
222 .id = I2C_DRIVERID_TVMIXER,
223 .detach_adapter = tvmixer_adapters,
224 .attach_adapter = tvmixer_adapters,
225 .detach_client = tvmixer_clients,
228 static const struct file_operations tvmixer_fops = {
229 .owner = THIS_MODULE,
230 .llseek = no_llseek,
231 .ioctl = tvmixer_ioctl,
232 .open = tvmixer_open,
233 .release = tvmixer_release,
236 /* ----------------------------------------------------------------------- */
238 static int tvmixer_adapters(struct i2c_adapter *adap)
240 struct i2c_client *client;
242 list_for_each_entry(client, &adap->clients, list)
243 tvmixer_clients(client);
244 return 0;
247 static int tvmixer_clients(struct i2c_client *client)
249 struct video_audio va;
250 int i,minor;
252 if (!(client->adapter->class & I2C_CLASS_TV_ANALOG))
253 return -1;
255 /* unregister ?? */
256 for (i = 0; i < DEV_MAX; i++) {
257 if (devices[i].dev == client) {
258 /* unregister */
259 unregister_sound_mixer(devices[i].minor);
260 devices[i].dev = NULL;
261 devices[i].minor = -1;
262 printk("tvmixer: %s unregistered (#1)\n",
263 client->name);
264 return 0;
268 /* look for a free slot */
269 for (i = 0; i < DEV_MAX; i++)
270 if (NULL == devices[i].dev)
271 break;
272 if (i == DEV_MAX) {
273 printk(KERN_WARNING "tvmixer: DEV_MAX too small\n");
274 return -1;
277 /* audio chip with mixer ??? */
278 if (NULL == client->driver->command)
279 return -1;
280 memset(&va,0,sizeof(va));
281 if (0 != client->driver->command(client,VIDIOCGAUDIO,&va))
282 return -1;
283 if (0 == (va.flags & VIDEO_AUDIO_VOLUME))
284 return -1;
286 /* everything is fine, register */
287 if ((minor = register_sound_mixer(&tvmixer_fops,devnr)) < 0) {
288 printk(KERN_ERR "tvmixer: cannot allocate mixer device\n");
289 return -1;
292 devices[i].minor = minor;
293 devices[i].count = 0;
294 devices[i].dev = client;
295 printk("tvmixer: %s (%s) registered with minor %d\n",
296 client->name,client->adapter->name,minor);
298 return 0;
301 /* ----------------------------------------------------------------------- */
303 static int __init tvmixer_init_module(void)
305 int i;
307 for (i = 0; i < DEV_MAX; i++)
308 devices[i].minor = -1;
310 return i2c_add_driver(&driver);
313 static void __exit tvmixer_cleanup_module(void)
315 int i;
317 i2c_del_driver(&driver);
318 for (i = 0; i < DEV_MAX; i++) {
319 if (devices[i].minor != -1) {
320 unregister_sound_mixer(devices[i].minor);
321 printk("tvmixer: %s unregistered (#2)\n",
322 devices[i].dev->name);
327 module_init(tvmixer_init_module);
328 module_exit(tvmixer_cleanup_module);
331 * Overrides for Emacs so that we follow Linus's tabbing style.
332 * ---------------------------------------------------------------------------
333 * Local variables:
334 * c-basic-offset: 8
335 * End: