VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / media / video / tvmixer.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/sched.h>
4 #include <linux/string.h>
5 #include <linux/timer.h>
6 #include <linux/delay.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/i2c.h>
10 #include <linux/videodev.h>
11 #include <linux/init.h>
12 #include <linux/kdev_t.h>
13 #include <linux/sound.h>
14 #include <linux/soundcard.h>
15
16 #include <asm/semaphore.h>
17 #include <asm/uaccess.h>
18
19
20 #define DEV_MAX  4
21
22 static int devnr = -1;
23 MODULE_PARM(devnr,"i");
24
25 MODULE_AUTHOR("Gerd Knorr");
26 MODULE_LICENSE("GPL");
27
28 /* ----------------------------------------------------------------------- */
29
30 struct TVMIXER {
31         struct i2c_client *dev;
32         int minor;
33         int count;
34 };
35
36 static struct TVMIXER devices[DEV_MAX];
37
38 static int tvmixer_adapters(struct i2c_adapter *adap);
39 static int tvmixer_clients(struct i2c_client *client);
40
41 /* ----------------------------------------------------------------------- */
42
43 static int mix_to_v4l(int i)
44 {
45         int r;
46
47         r = ((i & 0xff) * 65536 + 50) / 100;
48         if (r > 65535) r = 65535;
49         if (r <     0) r =     0;
50         return r;
51 }
52
53 static int v4l_to_mix(int i)
54 {
55         int r;
56
57         r = (i * 100 + 32768) / 65536;
58         if (r > 100) r = 100;
59         if (r <   0) r =   0;
60         return r | (r << 8);
61 }
62
63 static int v4l_to_mix2(int l, int r)
64 {
65         r = (r * 100 + 32768) / 65536;
66         if (r > 100) r = 100;
67         if (r <   0) r =   0;
68         l = (l * 100 + 32768) / 65536;
69         if (l > 100) l = 100;
70         if (l <   0) l =   0;
71         return (r << 8) | l;
72 }
73
74 static int tvmixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
75 {
76         struct video_audio va;
77         int left,right,ret,val = 0;
78         struct TVMIXER *mix = file->private_data;
79         struct i2c_client *client = mix->dev;
80         void __user *argp = (void __user *)arg;
81         int __user *p = argp;
82
83         if (NULL == client)
84                 return -ENODEV;
85         
86         if (cmd == SOUND_MIXER_INFO) {
87                 mixer_info info;
88                 strlcpy(info.id, "tv card", sizeof(info.id));
89                 strlcpy(info.name, i2c_clientname(client), sizeof(info.name));
90                 info.modify_counter = 42 /* FIXME */;
91                 if (copy_to_user(argp, &info, sizeof(info)))
92                         return -EFAULT;
93                 return 0;
94         }
95         if (cmd == SOUND_OLD_MIXER_INFO) {
96                 _old_mixer_info info;
97                 strlcpy(info.id, "tv card", sizeof(info.id));
98                 strlcpy(info.name, i2c_clientname(client), sizeof(info.name));
99                 if (copy_to_user(argp, &info, sizeof(info)))
100                         return -EFAULT;
101                 return 0;
102         }
103         if (cmd == OSS_GETVERSION)
104                 return put_user(SOUND_VERSION, p);
105
106         if (_SIOC_DIR(cmd) & _SIOC_WRITE)
107                 if (get_user(val, p))
108                         return -EFAULT;
109
110         /* read state */
111         memset(&va,0,sizeof(va));
112         client->driver->command(client,VIDIOCGAUDIO,&va);
113
114         switch (cmd) {
115         case MIXER_READ(SOUND_MIXER_RECMASK):
116         case MIXER_READ(SOUND_MIXER_CAPS):
117         case MIXER_READ(SOUND_MIXER_RECSRC):
118         case MIXER_WRITE(SOUND_MIXER_RECSRC):
119                 ret = 0;
120                 break;
121
122         case MIXER_READ(SOUND_MIXER_STEREODEVS):
123                 ret = SOUND_MASK_VOLUME;
124                 break;
125         case MIXER_READ(SOUND_MIXER_DEVMASK):
126                 ret = SOUND_MASK_VOLUME;
127                 if (va.flags & VIDEO_AUDIO_BASS)
128                         ret |= SOUND_MASK_BASS;
129                 if (va.flags & VIDEO_AUDIO_TREBLE)
130                         ret |= SOUND_MASK_TREBLE;
131                 break;
132
133         case MIXER_WRITE(SOUND_MIXER_VOLUME):
134                 left  = mix_to_v4l(val);
135                 right = mix_to_v4l(val >> 8);
136                 va.volume  = max(left,right);
137                 va.balance = (32768*min(left,right)) / (va.volume ? va.volume : 1);
138                 va.balance = (left<right) ? (65535-va.balance) : va.balance;
139                 if (va.volume)
140                         va.flags &= ~VIDEO_AUDIO_MUTE;
141                 client->driver->command(client,VIDIOCSAUDIO,&va);
142                 client->driver->command(client,VIDIOCGAUDIO,&va);
143                 /* fall throuth */
144         case MIXER_READ(SOUND_MIXER_VOLUME):
145                 left  = (min(65536 - va.balance,32768) *
146                          va.volume) / 32768;
147                 right = (min(va.balance,(u16)32768) *
148                          va.volume) / 32768;
149                 ret = v4l_to_mix2(left,right);
150                 break;
151                 
152         case MIXER_WRITE(SOUND_MIXER_BASS):
153                 va.bass = mix_to_v4l(val);
154                 client->driver->command(client,VIDIOCSAUDIO,&va);
155                 client->driver->command(client,VIDIOCGAUDIO,&va);
156                 /* fall throuth  */
157         case MIXER_READ(SOUND_MIXER_BASS):
158                 ret = v4l_to_mix(va.bass);
159                 break;
160
161         case MIXER_WRITE(SOUND_MIXER_TREBLE):
162                 va.treble = mix_to_v4l(val);
163                 client->driver->command(client,VIDIOCSAUDIO,&va);
164                 client->driver->command(client,VIDIOCGAUDIO,&va);
165                 /* fall throuth */
166         case MIXER_READ(SOUND_MIXER_TREBLE):
167                 ret = v4l_to_mix(va.treble);
168                 break;
169
170         default:
171                 return -EINVAL;
172         }
173         if (put_user(ret, p))
174                 return -EFAULT;
175         return 0;
176 }
177
178 static int tvmixer_open(struct inode *inode, struct file *file)
179 {
180         int i, minor = iminor(inode);
181         struct TVMIXER *mix = NULL;
182         struct i2c_client *client = NULL;
183
184         for (i = 0; i < DEV_MAX; i++) {
185                 if (devices[i].minor == minor) {
186                         mix = devices+i;
187                         client = mix->dev;
188                         break;
189                 }
190         }
191
192         if (NULL == client)
193                 return -ENODEV;
194
195         /* lock bttv in memory while the mixer is in use  */
196         file->private_data = mix;
197 #ifndef I2C_PEC
198         if (client->adapter->inc_use)
199                 client->adapter->inc_use(client->adapter);
200 #endif
201         if (client->adapter->owner)
202                 try_module_get(client->adapter->owner);
203         return 0;
204 }
205
206 static int tvmixer_release(struct inode *inode, struct file *file)
207 {
208         struct TVMIXER *mix = file->private_data;
209         struct i2c_client *client;
210
211         client = mix->dev;
212         if (NULL == client) {
213                 return -ENODEV;
214         }
215
216 #ifndef I2C_PEC
217         if (client->adapter->dec_use)
218                 client->adapter->dec_use(client->adapter);
219 #endif
220         if (client->adapter->owner)
221                 module_put(client->adapter->owner);
222         return 0;
223 }
224
225 static struct i2c_driver driver = {
226 #ifdef I2C_PEC
227         .owner           = THIS_MODULE,
228 #endif
229         .name            = "tv card mixer driver",
230         .id              = I2C_DRIVERID_TVMIXER,
231 #ifdef I2C_DF_DUMMY
232         .flags           = I2C_DF_DUMMY,
233 #else
234         .flags           = I2C_DF_NOTIFY,
235         .detach_adapter  = tvmixer_adapters,
236 #endif
237         .attach_adapter  = tvmixer_adapters,
238         .detach_client   = tvmixer_clients,
239 };
240
241 static struct file_operations tvmixer_fops = {
242         .owner          = THIS_MODULE,
243         .llseek         = no_llseek,
244         .ioctl          = tvmixer_ioctl,
245         .open           = tvmixer_open,
246         .release        = tvmixer_release,
247 };
248
249 /* ----------------------------------------------------------------------- */
250
251 static int tvmixer_adapters(struct i2c_adapter *adap)
252 {
253         struct list_head  *item;
254         struct i2c_client *client;
255
256         list_for_each(item,&adap->clients) {
257                 client = list_entry(item, struct i2c_client, list);
258                 tvmixer_clients(client);
259         }
260         return 0;
261 }
262
263 static int tvmixer_clients(struct i2c_client *client)
264 {
265         struct video_audio va;
266         int i,minor;
267
268 #ifdef I2C_CLASS_TV_ANALOG
269         if (!(client->adapter->class & I2C_CLASS_TV_ANALOG))
270                 return -1;
271 #else
272         /* TV card ??? */
273         switch (client->adapter->id) {
274         case I2C_ALGO_BIT | I2C_HW_SMBUS_VOODOO3:
275         case I2C_ALGO_BIT | I2C_HW_B_BT848:
276         case I2C_ALGO_BIT | I2C_HW_B_RIVA:
277                 /* ok, have a look ... */
278                 break;
279         default:
280                 /* ignore that one */
281                 return -1;
282         }
283 #endif
284
285         /* unregister ?? */
286         for (i = 0; i < DEV_MAX; i++) {
287                 if (devices[i].dev == client) {
288                         /* unregister */
289                         unregister_sound_mixer(devices[i].minor);
290                         devices[i].dev = NULL;
291                         devices[i].minor = -1;
292                         printk("tvmixer: %s unregistered (#1)\n",
293                                i2c_clientname(client));
294                         return 0;
295                 }
296         }
297
298         /* look for a free slot */
299         for (i = 0; i < DEV_MAX; i++)
300                 if (NULL == devices[i].dev)
301                         break;
302         if (i == DEV_MAX) {
303                 printk(KERN_WARNING "tvmixer: DEV_MAX too small\n");
304                 return -1;
305         }
306
307         /* audio chip with mixer ??? */
308         if (NULL == client->driver->command)
309                 return -1;
310         memset(&va,0,sizeof(va));
311         if (0 != client->driver->command(client,VIDIOCGAUDIO,&va))
312                 return -1;
313         if (0 == (va.flags & VIDEO_AUDIO_VOLUME))
314                 return -1;
315
316         /* everything is fine, register */
317         if ((minor = register_sound_mixer(&tvmixer_fops,devnr)) < 0) {
318                 printk(KERN_ERR "tvmixer: cannot allocate mixer device\n");
319                 return -1;
320         }
321
322         devices[i].minor = minor;
323         devices[i].count = 0;
324         devices[i].dev   = client;
325         printk("tvmixer: %s (%s) registered with minor %d\n",
326                client->name,client->adapter->name,minor);
327         
328         return 0;
329 }
330
331 /* ----------------------------------------------------------------------- */
332
333 static int tvmixer_init_module(void)
334 {
335         int i;
336         
337         for (i = 0; i < DEV_MAX; i++)
338                 devices[i].minor = -1;
339         i2c_add_driver(&driver);
340         return 0;
341 }
342
343 static void tvmixer_cleanup_module(void)
344 {
345         int i;
346         
347         i2c_del_driver(&driver);
348         for (i = 0; i < DEV_MAX; i++) {
349                 if (devices[i].minor != -1) {
350                         unregister_sound_mixer(devices[i].minor);
351                         printk("tvmixer: %s unregistered (#2)\n",
352                                i2c_clientname(devices[i].dev));
353                 }
354         }
355 }
356
357 module_init(tvmixer_init_module);
358 module_exit(tvmixer_cleanup_module);
359
360 /*
361  * Overrides for Emacs so that we follow Linus's tabbing style.
362  * ---------------------------------------------------------------------------
363  * Local variables:
364  * c-basic-offset: 8
365  * End:
366  */