1 #include <linux/module.h>
2 #include <linux/moduleparam.h>
3 #include <linux/kernel.h>
4 #include <linux/sched.h>
5 #include <linux/string.h>
6 #include <linux/timer.h>
7 #include <linux/delay.h>
8 #include <linux/errno.h>
9 #include <linux/slab.h>
10 #include <linux/i2c.h>
11 #include <linux/videodev.h>
12 #include <linux/init.h>
13 #include <linux/kdev_t.h>
14 #include <linux/sound.h>
15 #include <linux/soundcard.h>
17 #include <asm/semaphore.h>
18 #include <asm/uaccess.h>
23 static int devnr = -1;
24 module_param(devnr, int, 0644);
26 MODULE_AUTHOR("Gerd Knorr");
27 MODULE_LICENSE("GPL");
29 /* ----------------------------------------------------------------------- */
32 struct i2c_client *dev;
37 static struct TVMIXER devices[DEV_MAX];
39 static int tvmixer_adapters(struct i2c_adapter *adap);
40 static int tvmixer_clients(struct i2c_client *client);
42 /* ----------------------------------------------------------------------- */
44 static int mix_to_v4l(int i)
48 r = ((i & 0xff) * 65536 + 50) / 100;
49 if (r > 65535) r = 65535;
54 static int v4l_to_mix(int i)
58 r = (i * 100 + 32768) / 65536;
64 static int v4l_to_mix2(int l, int r)
66 r = (r * 100 + 32768) / 65536;
69 l = (l * 100 + 32768) / 65536;
75 static int tvmixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
77 struct video_audio va;
78 int left,right,ret,val = 0;
79 struct TVMIXER *mix = file->private_data;
80 struct i2c_client *client = mix->dev;
81 void __user *argp = (void __user *)arg;
87 if (cmd == SOUND_MIXER_INFO) {
89 strlcpy(info.id, "tv card", sizeof(info.id));
90 strlcpy(info.name, i2c_clientname(client), sizeof(info.name));
91 info.modify_counter = 42 /* FIXME */;
92 if (copy_to_user(argp, &info, sizeof(info)))
96 if (cmd == SOUND_OLD_MIXER_INFO) {
98 strlcpy(info.id, "tv card", sizeof(info.id));
99 strlcpy(info.name, i2c_clientname(client), sizeof(info.name));
100 if (copy_to_user(argp, &info, sizeof(info)))
104 if (cmd == OSS_GETVERSION)
105 return put_user(SOUND_VERSION, p);
107 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
108 if (get_user(val, p))
112 memset(&va,0,sizeof(va));
113 client->driver->command(client,VIDIOCGAUDIO,&va);
116 case MIXER_READ(SOUND_MIXER_RECMASK):
117 case MIXER_READ(SOUND_MIXER_CAPS):
118 case MIXER_READ(SOUND_MIXER_RECSRC):
119 case MIXER_WRITE(SOUND_MIXER_RECSRC):
123 case MIXER_READ(SOUND_MIXER_STEREODEVS):
124 ret = SOUND_MASK_VOLUME;
126 case MIXER_READ(SOUND_MIXER_DEVMASK):
127 ret = SOUND_MASK_VOLUME;
128 if (va.flags & VIDEO_AUDIO_BASS)
129 ret |= SOUND_MASK_BASS;
130 if (va.flags & VIDEO_AUDIO_TREBLE)
131 ret |= SOUND_MASK_TREBLE;
134 case MIXER_WRITE(SOUND_MIXER_VOLUME):
135 left = mix_to_v4l(val);
136 right = mix_to_v4l(val >> 8);
137 va.volume = max(left,right);
138 va.balance = (32768*min(left,right)) / (va.volume ? va.volume : 1);
139 va.balance = (left<right) ? (65535-va.balance) : va.balance;
141 va.flags &= ~VIDEO_AUDIO_MUTE;
142 client->driver->command(client,VIDIOCSAUDIO,&va);
143 client->driver->command(client,VIDIOCGAUDIO,&va);
145 case MIXER_READ(SOUND_MIXER_VOLUME):
146 left = (min(65536 - va.balance,32768) *
148 right = (min(va.balance,(u16)32768) *
150 ret = v4l_to_mix2(left,right);
153 case MIXER_WRITE(SOUND_MIXER_BASS):
154 va.bass = mix_to_v4l(val);
155 client->driver->command(client,VIDIOCSAUDIO,&va);
156 client->driver->command(client,VIDIOCGAUDIO,&va);
158 case MIXER_READ(SOUND_MIXER_BASS):
159 ret = v4l_to_mix(va.bass);
162 case MIXER_WRITE(SOUND_MIXER_TREBLE):
163 va.treble = mix_to_v4l(val);
164 client->driver->command(client,VIDIOCSAUDIO,&va);
165 client->driver->command(client,VIDIOCGAUDIO,&va);
167 case MIXER_READ(SOUND_MIXER_TREBLE):
168 ret = v4l_to_mix(va.treble);
174 if (put_user(ret, p))
179 static int tvmixer_open(struct inode *inode, struct file *file)
181 int i, minor = iminor(inode);
182 struct TVMIXER *mix = NULL;
183 struct i2c_client *client = NULL;
185 for (i = 0; i < DEV_MAX; i++) {
186 if (devices[i].minor == minor) {
196 /* lock bttv in memory while the mixer is in use */
197 file->private_data = mix;
199 if (client->adapter->inc_use)
200 client->adapter->inc_use(client->adapter);
202 if (client->adapter->owner)
203 try_module_get(client->adapter->owner);
207 static int tvmixer_release(struct inode *inode, struct file *file)
209 struct TVMIXER *mix = file->private_data;
210 struct i2c_client *client;
213 if (NULL == client) {
218 if (client->adapter->dec_use)
219 client->adapter->dec_use(client->adapter);
221 if (client->adapter->owner)
222 module_put(client->adapter->owner);
226 static struct i2c_driver driver = {
228 .owner = THIS_MODULE,
230 .name = "tv card mixer driver",
231 .id = I2C_DRIVERID_TVMIXER,
233 .flags = I2C_DF_DUMMY,
235 .flags = I2C_DF_NOTIFY,
236 .detach_adapter = tvmixer_adapters,
238 .attach_adapter = tvmixer_adapters,
239 .detach_client = tvmixer_clients,
242 static struct file_operations tvmixer_fops = {
243 .owner = THIS_MODULE,
245 .ioctl = tvmixer_ioctl,
246 .open = tvmixer_open,
247 .release = tvmixer_release,
250 /* ----------------------------------------------------------------------- */
252 static int tvmixer_adapters(struct i2c_adapter *adap)
254 struct list_head *item;
255 struct i2c_client *client;
257 list_for_each(item,&adap->clients) {
258 client = list_entry(item, struct i2c_client, list);
259 tvmixer_clients(client);
264 static int tvmixer_clients(struct i2c_client *client)
266 struct video_audio va;
269 #ifdef I2C_CLASS_TV_ANALOG
270 if (!(client->adapter->class & I2C_CLASS_TV_ANALOG))
274 switch (client->adapter->id) {
275 case I2C_ALGO_BIT | I2C_HW_SMBUS_VOODOO3:
276 case I2C_ALGO_BIT | I2C_HW_B_BT848:
277 case I2C_ALGO_BIT | I2C_HW_B_RIVA:
278 /* ok, have a look ... */
281 /* ignore that one */
287 for (i = 0; i < DEV_MAX; i++) {
288 if (devices[i].dev == client) {
290 unregister_sound_mixer(devices[i].minor);
291 devices[i].dev = NULL;
292 devices[i].minor = -1;
293 printk("tvmixer: %s unregistered (#1)\n",
294 i2c_clientname(client));
299 /* look for a free slot */
300 for (i = 0; i < DEV_MAX; i++)
301 if (NULL == devices[i].dev)
304 printk(KERN_WARNING "tvmixer: DEV_MAX too small\n");
308 /* audio chip with mixer ??? */
309 if (NULL == client->driver->command)
311 memset(&va,0,sizeof(va));
312 if (0 != client->driver->command(client,VIDIOCGAUDIO,&va))
314 if (0 == (va.flags & VIDEO_AUDIO_VOLUME))
317 /* everything is fine, register */
318 if ((minor = register_sound_mixer(&tvmixer_fops,devnr)) < 0) {
319 printk(KERN_ERR "tvmixer: cannot allocate mixer device\n");
323 devices[i].minor = minor;
324 devices[i].count = 0;
325 devices[i].dev = client;
326 printk("tvmixer: %s (%s) registered with minor %d\n",
327 client->name,client->adapter->name,minor);
332 /* ----------------------------------------------------------------------- */
334 static int __init tvmixer_init_module(void)
338 for (i = 0; i < DEV_MAX; i++)
339 devices[i].minor = -1;
341 return i2c_add_driver(&driver);
344 static void __exit tvmixer_cleanup_module(void)
348 i2c_del_driver(&driver);
349 for (i = 0; i < DEV_MAX; i++) {
350 if (devices[i].minor != -1) {
351 unregister_sound_mixer(devices[i].minor);
352 printk("tvmixer: %s unregistered (#2)\n",
353 i2c_clientname(devices[i].dev));
358 module_init(tvmixer_init_module);
359 module_exit(tvmixer_cleanup_module);
362 * Overrides for Emacs so that we follow Linus's tabbing style.
363 * ---------------------------------------------------------------------------