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