2 * $Id: tvmixer.c,v 1.8 2005/06/12 04:19:19 mchehab Exp $
5 #include <linux/module.h>
6 #include <linux/moduleparam.h>
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/string.h>
10 #include <linux/timer.h>
11 #include <linux/delay.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/videodev.h>
16 #include <linux/init.h>
17 #include <linux/kdev_t.h>
18 #include <linux/sound.h>
19 #include <linux/soundcard.h>
21 #include <asm/semaphore.h>
22 #include <asm/uaccess.h>
27 static int devnr
= -1;
28 module_param(devnr
, int, 0644);
30 MODULE_AUTHOR("Gerd Knorr");
31 MODULE_LICENSE("GPL");
33 /* ----------------------------------------------------------------------- */
36 struct i2c_client
*dev
;
41 static struct TVMIXER devices
[DEV_MAX
];
43 static int tvmixer_adapters(struct i2c_adapter
*adap
);
44 static int tvmixer_clients(struct i2c_client
*client
);
46 /* ----------------------------------------------------------------------- */
48 static int mix_to_v4l(int i
)
52 r
= ((i
& 0xff) * 65536 + 50) / 100;
53 if (r
> 65535) r
= 65535;
58 static int v4l_to_mix(int i
)
62 r
= (i
* 100 + 32768) / 65536;
68 static int v4l_to_mix2(int l
, int r
)
70 r
= (r
* 100 + 32768) / 65536;
73 l
= (l
* 100 + 32768) / 65536;
79 static int tvmixer_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
81 struct video_audio va
;
82 int left
,right
,ret
,val
= 0;
83 struct TVMIXER
*mix
= file
->private_data
;
84 struct i2c_client
*client
= mix
->dev
;
85 void __user
*argp
= (void __user
*)arg
;
91 if (cmd
== SOUND_MIXER_INFO
) {
93 strlcpy(info
.id
, "tv card", sizeof(info
.id
));
94 strlcpy(info
.name
, client
->name
, sizeof(info
.name
));
95 info
.modify_counter
= 42 /* FIXME */;
96 if (copy_to_user(argp
, &info
, sizeof(info
)))
100 if (cmd
== SOUND_OLD_MIXER_INFO
) {
101 _old_mixer_info info
;
102 strlcpy(info
.id
, "tv card", sizeof(info
.id
));
103 strlcpy(info
.name
, client
->name
, sizeof(info
.name
));
104 if (copy_to_user(argp
, &info
, sizeof(info
)))
108 if (cmd
== OSS_GETVERSION
)
109 return put_user(SOUND_VERSION
, p
);
111 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
112 if (get_user(val
, p
))
116 memset(&va
,0,sizeof(va
));
117 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
120 case MIXER_READ(SOUND_MIXER_RECMASK
):
121 case MIXER_READ(SOUND_MIXER_CAPS
):
122 case MIXER_READ(SOUND_MIXER_RECSRC
):
123 case MIXER_WRITE(SOUND_MIXER_RECSRC
):
127 case MIXER_READ(SOUND_MIXER_STEREODEVS
):
128 ret
= SOUND_MASK_VOLUME
;
130 case MIXER_READ(SOUND_MIXER_DEVMASK
):
131 ret
= SOUND_MASK_VOLUME
;
132 if (va
.flags
& VIDEO_AUDIO_BASS
)
133 ret
|= SOUND_MASK_BASS
;
134 if (va
.flags
& VIDEO_AUDIO_TREBLE
)
135 ret
|= SOUND_MASK_TREBLE
;
138 case MIXER_WRITE(SOUND_MIXER_VOLUME
):
139 left
= mix_to_v4l(val
);
140 right
= mix_to_v4l(val
>> 8);
141 va
.volume
= max(left
,right
);
142 va
.balance
= (32768*min(left
,right
)) / (va
.volume
? va
.volume
: 1);
143 va
.balance
= (left
<right
) ? (65535-va
.balance
) : va
.balance
;
145 va
.flags
&= ~VIDEO_AUDIO_MUTE
;
146 client
->driver
->command(client
,VIDIOCSAUDIO
,&va
);
147 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
149 case MIXER_READ(SOUND_MIXER_VOLUME
):
150 left
= (min(65536 - va
.balance
,32768) *
152 right
= (min(va
.balance
,(u16
)32768) *
154 ret
= v4l_to_mix2(left
,right
);
157 case MIXER_WRITE(SOUND_MIXER_BASS
):
158 va
.bass
= mix_to_v4l(val
);
159 client
->driver
->command(client
,VIDIOCSAUDIO
,&va
);
160 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
162 case MIXER_READ(SOUND_MIXER_BASS
):
163 ret
= v4l_to_mix(va
.bass
);
166 case MIXER_WRITE(SOUND_MIXER_TREBLE
):
167 va
.treble
= mix_to_v4l(val
);
168 client
->driver
->command(client
,VIDIOCSAUDIO
,&va
);
169 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
171 case MIXER_READ(SOUND_MIXER_TREBLE
):
172 ret
= v4l_to_mix(va
.treble
);
178 if (put_user(ret
, p
))
183 static int tvmixer_open(struct inode
*inode
, struct file
*file
)
185 int i
, minor
= iminor(inode
);
186 struct TVMIXER
*mix
= NULL
;
187 struct i2c_client
*client
= NULL
;
189 for (i
= 0; i
< DEV_MAX
; i
++) {
190 if (devices
[i
].minor
== minor
) {
200 /* lock bttv in memory while the mixer is in use */
201 file
->private_data
= mix
;
203 if (client
->adapter
->inc_use
)
204 client
->adapter
->inc_use(client
->adapter
);
206 if (client
->adapter
->owner
)
207 try_module_get(client
->adapter
->owner
);
211 static int tvmixer_release(struct inode
*inode
, struct file
*file
)
213 struct TVMIXER
*mix
= file
->private_data
;
214 struct i2c_client
*client
;
217 if (NULL
== client
) {
222 if (client
->adapter
->dec_use
)
223 client
->adapter
->dec_use(client
->adapter
);
225 if (client
->adapter
->owner
)
226 module_put(client
->adapter
->owner
);
230 static struct i2c_driver driver
= {
232 .owner
= THIS_MODULE
,
234 .name
= "tv card mixer driver",
235 .id
= I2C_DRIVERID_TVMIXER
,
237 .flags
= I2C_DF_DUMMY
,
239 .flags
= I2C_DF_NOTIFY
,
240 .detach_adapter
= tvmixer_adapters
,
242 .attach_adapter
= tvmixer_adapters
,
243 .detach_client
= tvmixer_clients
,
246 static struct file_operations tvmixer_fops
= {
247 .owner
= THIS_MODULE
,
249 .ioctl
= tvmixer_ioctl
,
250 .open
= tvmixer_open
,
251 .release
= tvmixer_release
,
254 /* ----------------------------------------------------------------------- */
256 static int tvmixer_adapters(struct i2c_adapter
*adap
)
258 struct list_head
*item
;
259 struct i2c_client
*client
;
261 list_for_each(item
,&adap
->clients
) {
262 client
= list_entry(item
, struct i2c_client
, list
);
263 tvmixer_clients(client
);
268 static int tvmixer_clients(struct i2c_client
*client
)
270 struct video_audio va
;
273 #ifdef I2C_CLASS_TV_ANALOG
274 if (!(client
->adapter
->class & I2C_CLASS_TV_ANALOG
))
278 switch (client
->adapter
->id
) {
279 case I2C_HW_SMBUS_VOODOO3
:
282 /* ok, have a look ... */
285 /* ignore that one */
291 for (i
= 0; i
< DEV_MAX
; i
++) {
292 if (devices
[i
].dev
== client
) {
294 unregister_sound_mixer(devices
[i
].minor
);
295 devices
[i
].dev
= NULL
;
296 devices
[i
].minor
= -1;
297 printk("tvmixer: %s unregistered (#1)\n",
303 /* look for a free slot */
304 for (i
= 0; i
< DEV_MAX
; i
++)
305 if (NULL
== devices
[i
].dev
)
308 printk(KERN_WARNING
"tvmixer: DEV_MAX too small\n");
312 /* audio chip with mixer ??? */
313 if (NULL
== client
->driver
->command
)
315 memset(&va
,0,sizeof(va
));
316 if (0 != client
->driver
->command(client
,VIDIOCGAUDIO
,&va
))
318 if (0 == (va
.flags
& VIDEO_AUDIO_VOLUME
))
321 /* everything is fine, register */
322 if ((minor
= register_sound_mixer(&tvmixer_fops
,devnr
)) < 0) {
323 printk(KERN_ERR
"tvmixer: cannot allocate mixer device\n");
327 devices
[i
].minor
= minor
;
328 devices
[i
].count
= 0;
329 devices
[i
].dev
= client
;
330 printk("tvmixer: %s (%s) registered with minor %d\n",
331 client
->name
,client
->adapter
->name
,minor
);
336 /* ----------------------------------------------------------------------- */
338 static int __init
tvmixer_init_module(void)
342 for (i
= 0; i
< DEV_MAX
; i
++)
343 devices
[i
].minor
= -1;
345 return i2c_add_driver(&driver
);
348 static void __exit
tvmixer_cleanup_module(void)
352 i2c_del_driver(&driver
);
353 for (i
= 0; i
< DEV_MAX
; i
++) {
354 if (devices
[i
].minor
!= -1) {
355 unregister_sound_mixer(devices
[i
].minor
);
356 printk("tvmixer: %s unregistered (#2)\n",
357 devices
[i
].dev
->name
);
362 module_init(tvmixer_init_module
);
363 module_exit(tvmixer_cleanup_module
);
366 * Overrides for Emacs so that we follow Linus's tabbing style.
367 * ---------------------------------------------------------------------------