V4L/DVB (6225): AverTV Studio 307 has only one composite input
[linux-2.6] / drivers / media / video / tvmixer.c
1 /*
2  */
3
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>
17
18 #include <asm/semaphore.h>
19 #include <asm/uaccess.h>
20
21
22 #define DEV_MAX  4
23
24 static int devnr = -1;
25 module_param(devnr, int, 0644);
26
27 MODULE_AUTHOR("Gerd Knorr");
28 MODULE_LICENSE("GPL");
29
30 /* ----------------------------------------------------------------------- */
31
32 struct TVMIXER {
33         struct i2c_client *dev;
34         int minor;
35         int count;
36 };
37
38 static struct TVMIXER devices[DEV_MAX];
39
40 static int tvmixer_adapters(struct i2c_adapter *adap);
41 static int tvmixer_clients(struct i2c_client *client);
42
43 /* ----------------------------------------------------------------------- */
44
45 static int mix_to_v4l(int i)
46 {
47         int r;
48
49         r = ((i & 0xff) * 65536 + 50) / 100;
50         if (r > 65535) r = 65535;
51         if (r <     0) r =     0;
52         return r;
53 }
54
55 static int v4l_to_mix(int i)
56 {
57         int r;
58
59         r = (i * 100 + 32768) / 65536;
60         if (r > 100) r = 100;
61         if (r <   0) r =   0;
62         return r | (r << 8);
63 }
64
65 static int v4l_to_mix2(int l, int r)
66 {
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;
74 }
75
76 static int tvmixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
77 {
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;
84
85         if (NULL == client)
86                 return -ENODEV;
87
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;
96         }
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;
104         }
105         if (cmd == OSS_GETVERSION)
106                 return put_user(SOUND_VERSION, p);
107
108         if (_SIOC_DIR(cmd) & _SIOC_WRITE)
109                 if (get_user(val, p))
110                         return -EFAULT;
111
112         /* read state */
113         memset(&va,0,sizeof(va));
114         client->driver->command(client,VIDIOCGAUDIO,&va);
115
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;
123
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;
134
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;
153
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;
162
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;
171
172         default:
173                 return -EINVAL;
174         }
175         if (put_user(ret, p))
176                 return -EFAULT;
177         return 0;
178 }
179
180 static int tvmixer_open(struct inode *inode, struct file *file)
181 {
182         int i, minor = iminor(inode);
183         struct TVMIXER *mix = NULL;
184         struct i2c_client *client = NULL;
185
186         for (i = 0; i < DEV_MAX; i++) {
187                 if (devices[i].minor == minor) {
188                         mix = devices+i;
189                         client = mix->dev;
190                         break;
191                 }
192         }
193
194         if (NULL == client)
195                 return -ENODEV;
196
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;
202 }
203
204 static int tvmixer_release(struct inode *inode, struct file *file)
205 {
206         struct TVMIXER *mix = file->private_data;
207         struct i2c_client *client;
208
209         client = mix->dev;
210         if (NULL == client) {
211                 return -ENODEV;
212         }
213
214         module_put(client->adapter->owner);
215         return 0;
216 }
217
218 static struct i2c_driver driver = {
219         .driver = {
220                 .name    = "tvmixer",
221         },
222         .id              = I2C_DRIVERID_TVMIXER,
223         .detach_adapter  = tvmixer_adapters,
224         .attach_adapter  = tvmixer_adapters,
225         .detach_client   = tvmixer_clients,
226 };
227
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,
234 };
235
236 /* ----------------------------------------------------------------------- */
237
238 static int tvmixer_adapters(struct i2c_adapter *adap)
239 {
240         struct list_head  *item;
241         struct i2c_client *client;
242
243         list_for_each(item,&adap->clients) {
244                 client = list_entry(item, struct i2c_client, list);
245                 tvmixer_clients(client);
246         }
247         return 0;
248 }
249
250 static int tvmixer_clients(struct i2c_client *client)
251 {
252         struct video_audio va;
253         int i,minor;
254
255         if (!(client->adapter->class & I2C_CLASS_TV_ANALOG))
256                 return -1;
257
258         /* unregister ?? */
259         for (i = 0; i < DEV_MAX; i++) {
260                 if (devices[i].dev == client) {
261                         /* unregister */
262                         unregister_sound_mixer(devices[i].minor);
263                         devices[i].dev = NULL;
264                         devices[i].minor = -1;
265                         printk("tvmixer: %s unregistered (#1)\n",
266                                client->name);
267                         return 0;
268                 }
269         }
270
271         /* look for a free slot */
272         for (i = 0; i < DEV_MAX; i++)
273                 if (NULL == devices[i].dev)
274                         break;
275         if (i == DEV_MAX) {
276                 printk(KERN_WARNING "tvmixer: DEV_MAX too small\n");
277                 return -1;
278         }
279
280         /* audio chip with mixer ??? */
281         if (NULL == client->driver->command)
282                 return -1;
283         memset(&va,0,sizeof(va));
284         if (0 != client->driver->command(client,VIDIOCGAUDIO,&va))
285                 return -1;
286         if (0 == (va.flags & VIDEO_AUDIO_VOLUME))
287                 return -1;
288
289         /* everything is fine, register */
290         if ((minor = register_sound_mixer(&tvmixer_fops,devnr)) < 0) {
291                 printk(KERN_ERR "tvmixer: cannot allocate mixer device\n");
292                 return -1;
293         }
294
295         devices[i].minor = minor;
296         devices[i].count = 0;
297         devices[i].dev   = client;
298         printk("tvmixer: %s (%s) registered with minor %d\n",
299                client->name,client->adapter->name,minor);
300
301         return 0;
302 }
303
304 /* ----------------------------------------------------------------------- */
305
306 static int __init tvmixer_init_module(void)
307 {
308         int i;
309
310         for (i = 0; i < DEV_MAX; i++)
311                 devices[i].minor = -1;
312
313         return i2c_add_driver(&driver);
314 }
315
316 static void __exit tvmixer_cleanup_module(void)
317 {
318         int i;
319
320         i2c_del_driver(&driver);
321         for (i = 0; i < DEV_MAX; i++) {
322                 if (devices[i].minor != -1) {
323                         unregister_sound_mixer(devices[i].minor);
324                         printk("tvmixer: %s unregistered (#2)\n",
325                                devices[i].dev->name);
326                 }
327         }
328 }
329
330 module_init(tvmixer_init_module);
331 module_exit(tvmixer_cleanup_module);
332
333 /*
334  * Overrides for Emacs so that we follow Linus's tabbing style.
335  * ---------------------------------------------------------------------------
336  * Local variables:
337  * c-basic-offset: 8
338  * End:
339  */