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>
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>
16 #include <asm/semaphore.h>
17 #include <asm/uaccess.h>
22 static int devnr = -1;
23 MODULE_PARM(devnr,"i");
25 MODULE_AUTHOR("Gerd Knorr");
26 MODULE_LICENSE("GPL");
28 /* ----------------------------------------------------------------------- */
31 struct i2c_client *dev;
36 static struct TVMIXER devices[DEV_MAX];
38 static int tvmixer_adapters(struct i2c_adapter *adap);
39 static int tvmixer_clients(struct i2c_client *client);
41 /* ----------------------------------------------------------------------- */
43 static int mix_to_v4l(int i)
47 r = ((i & 0xff) * 65536 + 50) / 100;
48 if (r > 65535) r = 65535;
53 static int v4l_to_mix(int i)
57 r = (i * 100 + 32768) / 65536;
63 static int v4l_to_mix2(int l, int r)
65 r = (r * 100 + 32768) / 65536;
68 l = (l * 100 + 32768) / 65536;
74 static int tvmixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
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;
86 if (cmd == SOUND_MIXER_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)))
95 if (cmd == SOUND_OLD_MIXER_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)))
103 if (cmd == OSS_GETVERSION)
104 return put_user(SOUND_VERSION, p);
106 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
107 if (get_user(val, p))
111 memset(&va,0,sizeof(va));
112 client->driver->command(client,VIDIOCGAUDIO,&va);
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):
122 case MIXER_READ(SOUND_MIXER_STEREODEVS):
123 ret = SOUND_MASK_VOLUME;
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;
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;
140 va.flags &= ~VIDEO_AUDIO_MUTE;
141 client->driver->command(client,VIDIOCSAUDIO,&va);
142 client->driver->command(client,VIDIOCGAUDIO,&va);
144 case MIXER_READ(SOUND_MIXER_VOLUME):
145 left = (min(65536 - va.balance,32768) *
147 right = (min(va.balance,(u16)32768) *
149 ret = v4l_to_mix2(left,right);
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);
157 case MIXER_READ(SOUND_MIXER_BASS):
158 ret = v4l_to_mix(va.bass);
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);
166 case MIXER_READ(SOUND_MIXER_TREBLE):
167 ret = v4l_to_mix(va.treble);
173 if (put_user(ret, p))
178 static int tvmixer_open(struct inode *inode, struct file *file)
180 int i, minor = iminor(inode);
181 struct TVMIXER *mix = NULL;
182 struct i2c_client *client = NULL;
184 for (i = 0; i < DEV_MAX; i++) {
185 if (devices[i].minor == minor) {
195 /* lock bttv in memory while the mixer is in use */
196 file->private_data = mix;
198 if (client->adapter->inc_use)
199 client->adapter->inc_use(client->adapter);
201 if (client->adapter->owner)
202 try_module_get(client->adapter->owner);
206 static int tvmixer_release(struct inode *inode, struct file *file)
208 struct TVMIXER *mix = file->private_data;
209 struct i2c_client *client;
212 if (NULL == client) {
217 if (client->adapter->dec_use)
218 client->adapter->dec_use(client->adapter);
220 if (client->adapter->owner)
221 module_put(client->adapter->owner);
225 static struct i2c_driver driver = {
227 .owner = THIS_MODULE,
229 .name = "tv card mixer driver",
230 .id = I2C_DRIVERID_TVMIXER,
232 .flags = I2C_DF_DUMMY,
234 .flags = I2C_DF_NOTIFY,
235 .detach_adapter = tvmixer_adapters,
237 .attach_adapter = tvmixer_adapters,
238 .detach_client = tvmixer_clients,
241 static struct file_operations tvmixer_fops = {
242 .owner = THIS_MODULE,
244 .ioctl = tvmixer_ioctl,
245 .open = tvmixer_open,
246 .release = tvmixer_release,
249 /* ----------------------------------------------------------------------- */
251 static int tvmixer_adapters(struct i2c_adapter *adap)
253 struct list_head *item;
254 struct i2c_client *client;
256 list_for_each(item,&adap->clients) {
257 client = list_entry(item, struct i2c_client, list);
258 tvmixer_clients(client);
263 static int tvmixer_clients(struct i2c_client *client)
265 struct video_audio va;
268 #ifdef I2C_CLASS_TV_ANALOG
269 if (!(client->adapter->class & I2C_CLASS_TV_ANALOG))
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 ... */
280 /* ignore that one */
286 for (i = 0; i < DEV_MAX; i++) {
287 if (devices[i].dev == client) {
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));
298 /* look for a free slot */
299 for (i = 0; i < DEV_MAX; i++)
300 if (NULL == devices[i].dev)
303 printk(KERN_WARNING "tvmixer: DEV_MAX too small\n");
307 /* audio chip with mixer ??? */
308 if (NULL == client->driver->command)
310 memset(&va,0,sizeof(va));
311 if (0 != client->driver->command(client,VIDIOCGAUDIO,&va))
313 if (0 == (va.flags & VIDEO_AUDIO_VOLUME))
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");
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);
331 /* ----------------------------------------------------------------------- */
333 static int tvmixer_init_module(void)
337 for (i = 0; i < DEV_MAX; i++)
338 devices[i].minor = -1;
339 i2c_add_driver(&driver);
343 static void tvmixer_cleanup_module(void)
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));
357 module_init(tvmixer_init_module);
358 module_exit(tvmixer_cleanup_module);
361 * Overrides for Emacs so that we follow Linus's tabbing style.
362 * ---------------------------------------------------------------------------