4 #include <linux/module.h>
5 #include <linux/moduleparam.h>
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/timer.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/videodev.h>
14 #include <linux/init.h>
15 #include <linux/kdev_t.h>
16 #include <linux/sound.h>
17 #include <linux/soundcard.h>
19 #include <asm/semaphore.h>
20 #include <asm/uaccess.h>
25 static int devnr = -1;
26 module_param(devnr, int, 0644);
28 MODULE_AUTHOR("Gerd Knorr");
29 MODULE_LICENSE("GPL");
31 /* ----------------------------------------------------------------------- */
34 struct i2c_client *dev;
39 static struct TVMIXER devices[DEV_MAX];
41 static int tvmixer_adapters(struct i2c_adapter *adap);
42 static int tvmixer_clients(struct i2c_client *client);
44 /* ----------------------------------------------------------------------- */
46 static int mix_to_v4l(int i)
50 r = ((i & 0xff) * 65536 + 50) / 100;
51 if (r > 65535) r = 65535;
56 static int v4l_to_mix(int i)
60 r = (i * 100 + 32768) / 65536;
66 static int v4l_to_mix2(int l, int r)
68 r = (r * 100 + 32768) / 65536;
71 l = (l * 100 + 32768) / 65536;
77 static int tvmixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
79 struct video_audio va;
80 int left,right,ret,val = 0;
81 struct TVMIXER *mix = file->private_data;
82 struct i2c_client *client = mix->dev;
83 void __user *argp = (void __user *)arg;
89 if (cmd == SOUND_MIXER_INFO) {
91 strlcpy(info.id, "tv card", sizeof(info.id));
92 strlcpy(info.name, client->name, sizeof(info.name));
93 info.modify_counter = 42 /* FIXME */;
94 if (copy_to_user(argp, &info, sizeof(info)))
98 if (cmd == SOUND_OLD_MIXER_INFO) {
100 strlcpy(info.id, "tv card", sizeof(info.id));
101 strlcpy(info.name, client->name, sizeof(info.name));
102 if (copy_to_user(argp, &info, sizeof(info)))
106 if (cmd == OSS_GETVERSION)
107 return put_user(SOUND_VERSION, p);
109 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
110 if (get_user(val, p))
114 memset(&va,0,sizeof(va));
115 client->driver->command(client,VIDIOCGAUDIO,&va);
118 case MIXER_READ(SOUND_MIXER_RECMASK):
119 case MIXER_READ(SOUND_MIXER_CAPS):
120 case MIXER_READ(SOUND_MIXER_RECSRC):
121 case MIXER_WRITE(SOUND_MIXER_RECSRC):
125 case MIXER_READ(SOUND_MIXER_STEREODEVS):
126 ret = SOUND_MASK_VOLUME;
128 case MIXER_READ(SOUND_MIXER_DEVMASK):
129 ret = SOUND_MASK_VOLUME;
130 if (va.flags & VIDEO_AUDIO_BASS)
131 ret |= SOUND_MASK_BASS;
132 if (va.flags & VIDEO_AUDIO_TREBLE)
133 ret |= SOUND_MASK_TREBLE;
136 case MIXER_WRITE(SOUND_MIXER_VOLUME):
137 left = mix_to_v4l(val);
138 right = mix_to_v4l(val >> 8);
139 va.volume = max(left,right);
140 va.balance = (32768*min(left,right)) / (va.volume ? va.volume : 1);
141 va.balance = (left<right) ? (65535-va.balance) : va.balance;
143 va.flags &= ~VIDEO_AUDIO_MUTE;
144 client->driver->command(client,VIDIOCSAUDIO,&va);
145 client->driver->command(client,VIDIOCGAUDIO,&va);
147 case MIXER_READ(SOUND_MIXER_VOLUME):
148 left = (min(65536 - va.balance,32768) *
150 right = (min(va.balance,(u16)32768) *
152 ret = v4l_to_mix2(left,right);
155 case MIXER_WRITE(SOUND_MIXER_BASS):
156 va.bass = mix_to_v4l(val);
157 client->driver->command(client,VIDIOCSAUDIO,&va);
158 client->driver->command(client,VIDIOCGAUDIO,&va);
160 case MIXER_READ(SOUND_MIXER_BASS):
161 ret = v4l_to_mix(va.bass);
164 case MIXER_WRITE(SOUND_MIXER_TREBLE):
165 va.treble = mix_to_v4l(val);
166 client->driver->command(client,VIDIOCSAUDIO,&va);
167 client->driver->command(client,VIDIOCGAUDIO,&va);
169 case MIXER_READ(SOUND_MIXER_TREBLE):
170 ret = v4l_to_mix(va.treble);
176 if (put_user(ret, p))
181 static int tvmixer_open(struct inode *inode, struct file *file)
183 int i, minor = iminor(inode);
184 struct TVMIXER *mix = NULL;
185 struct i2c_client *client = NULL;
187 for (i = 0; i < DEV_MAX; i++) {
188 if (devices[i].minor == minor) {
198 /* lock bttv in memory while the mixer is in use */
199 file->private_data = mix;
200 if (client->adapter->owner)
201 try_module_get(client->adapter->owner);
205 static int tvmixer_release(struct inode *inode, struct file *file)
207 struct TVMIXER *mix = file->private_data;
208 struct i2c_client *client;
211 if (NULL == client) {
215 module_put(client->adapter->owner);
219 static struct i2c_driver driver = {
223 .id = I2C_DRIVERID_TVMIXER,
224 .detach_adapter = tvmixer_adapters,
225 .attach_adapter = tvmixer_adapters,
226 .detach_client = tvmixer_clients,
229 static const struct file_operations tvmixer_fops = {
230 .owner = THIS_MODULE,
232 .ioctl = tvmixer_ioctl,
233 .open = tvmixer_open,
234 .release = tvmixer_release,
237 /* ----------------------------------------------------------------------- */
239 static int tvmixer_adapters(struct i2c_adapter *adap)
241 struct list_head *item;
242 struct i2c_client *client;
244 list_for_each(item,&adap->clients) {
245 client = list_entry(item, struct i2c_client, list);
246 tvmixer_clients(client);
251 static int tvmixer_clients(struct i2c_client *client)
253 struct video_audio va;
256 if (!(client->adapter->class & I2C_CLASS_TV_ANALOG))
260 for (i = 0; i < DEV_MAX; i++) {
261 if (devices[i].dev == client) {
263 unregister_sound_mixer(devices[i].minor);
264 devices[i].dev = NULL;
265 devices[i].minor = -1;
266 printk("tvmixer: %s unregistered (#1)\n",
272 /* look for a free slot */
273 for (i = 0; i < DEV_MAX; i++)
274 if (NULL == devices[i].dev)
277 printk(KERN_WARNING "tvmixer: DEV_MAX too small\n");
281 /* audio chip with mixer ??? */
282 if (NULL == client->driver->command)
284 memset(&va,0,sizeof(va));
285 if (0 != client->driver->command(client,VIDIOCGAUDIO,&va))
287 if (0 == (va.flags & VIDEO_AUDIO_VOLUME))
290 /* everything is fine, register */
291 if ((minor = register_sound_mixer(&tvmixer_fops,devnr)) < 0) {
292 printk(KERN_ERR "tvmixer: cannot allocate mixer device\n");
296 devices[i].minor = minor;
297 devices[i].count = 0;
298 devices[i].dev = client;
299 printk("tvmixer: %s (%s) registered with minor %d\n",
300 client->name,client->adapter->name,minor);
305 /* ----------------------------------------------------------------------- */
307 static int __init tvmixer_init_module(void)
311 for (i = 0; i < DEV_MAX; i++)
312 devices[i].minor = -1;
314 return i2c_add_driver(&driver);
317 static void __exit tvmixer_cleanup_module(void)
321 i2c_del_driver(&driver);
322 for (i = 0; i < DEV_MAX; i++) {
323 if (devices[i].minor != -1) {
324 unregister_sound_mixer(devices[i].minor);
325 printk("tvmixer: %s unregistered (#2)\n",
326 devices[i].dev->name);
331 module_init(tvmixer_init_module);
332 module_exit(tvmixer_cleanup_module);
335 * Overrides for Emacs so that we follow Linus's tabbing style.
336 * ---------------------------------------------------------------------------