Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[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 i2c_client *client;
241
242         list_for_each_entry(client, &adap->clients, list)
243                 tvmixer_clients(client);
244         return 0;
245 }
246
247 static int tvmixer_clients(struct i2c_client *client)
248 {
249         struct video_audio va;
250         int i,minor;
251
252         if (!(client->adapter->class & I2C_CLASS_TV_ANALOG))
253                 return -1;
254
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;
265                 }
266         }
267
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;
275         }
276
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;
285
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;
290         }
291
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);
297
298         return 0;
299 }
300
301 /* ----------------------------------------------------------------------- */
302
303 static int __init tvmixer_init_module(void)
304 {
305         int i;
306
307         for (i = 0; i < DEV_MAX; i++)
308                 devices[i].minor = -1;
309
310         return i2c_add_driver(&driver);
311 }
312
313 static void __exit tvmixer_cleanup_module(void)
314 {
315         int i;
316
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);
323                 }
324         }
325 }
326
327 module_init(tvmixer_init_module);
328 module_exit(tvmixer_cleanup_module);
329
330 /*
331  * Overrides for Emacs so that we follow Linus's tabbing style.
332  * ---------------------------------------------------------------------------
333  * Local variables:
334  * c-basic-offset: 8
335  * End:
336  */