4 #include <linux/module.h>
5 #include <linux/moduleparam.h>
6 #include <linux/kernel.h>
7 #include <linux/sched.h>
8 #include <linux/string.h>
9 #include <linux/timer.h>
10 #include <linux/delay.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/videodev.h>
15 #include <linux/init.h>
16 #include <linux/kdev_t.h>
17 #include <linux/sound.h>
18 #include <linux/soundcard.h>
20 #include <asm/semaphore.h>
21 #include <asm/uaccess.h>
26 static int devnr
= -1;
27 module_param(devnr
, int, 0644);
29 MODULE_AUTHOR("Gerd Knorr");
30 MODULE_LICENSE("GPL");
32 /* ----------------------------------------------------------------------- */
35 struct i2c_client
*dev
;
40 static struct TVMIXER devices
[DEV_MAX
];
42 static int tvmixer_adapters(struct i2c_adapter
*adap
);
43 static int tvmixer_clients(struct i2c_client
*client
);
45 /* ----------------------------------------------------------------------- */
47 static int mix_to_v4l(int i
)
51 r
= ((i
& 0xff) * 65536 + 50) / 100;
52 if (r
> 65535) r
= 65535;
57 static int v4l_to_mix(int i
)
61 r
= (i
* 100 + 32768) / 65536;
67 static int v4l_to_mix2(int l
, int r
)
69 r
= (r
* 100 + 32768) / 65536;
72 l
= (l
* 100 + 32768) / 65536;
78 static int tvmixer_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
80 struct video_audio va
;
81 int left
,right
,ret
,val
= 0;
82 struct TVMIXER
*mix
= file
->private_data
;
83 struct i2c_client
*client
= mix
->dev
;
84 void __user
*argp
= (void __user
*)arg
;
90 if (cmd
== SOUND_MIXER_INFO
) {
92 strlcpy(info
.id
, "tv card", sizeof(info
.id
));
93 strlcpy(info
.name
, client
->name
, sizeof(info
.name
));
94 info
.modify_counter
= 42 /* FIXME */;
95 if (copy_to_user(argp
, &info
, sizeof(info
)))
99 if (cmd
== SOUND_OLD_MIXER_INFO
) {
100 _old_mixer_info info
;
101 strlcpy(info
.id
, "tv card", sizeof(info
.id
));
102 strlcpy(info
.name
, client
->name
, sizeof(info
.name
));
103 if (copy_to_user(argp
, &info
, sizeof(info
)))
107 if (cmd
== OSS_GETVERSION
)
108 return put_user(SOUND_VERSION
, p
);
110 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
111 if (get_user(val
, p
))
115 memset(&va
,0,sizeof(va
));
116 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
119 case MIXER_READ(SOUND_MIXER_RECMASK
):
120 case MIXER_READ(SOUND_MIXER_CAPS
):
121 case MIXER_READ(SOUND_MIXER_RECSRC
):
122 case MIXER_WRITE(SOUND_MIXER_RECSRC
):
126 case MIXER_READ(SOUND_MIXER_STEREODEVS
):
127 ret
= SOUND_MASK_VOLUME
;
129 case MIXER_READ(SOUND_MIXER_DEVMASK
):
130 ret
= SOUND_MASK_VOLUME
;
131 if (va
.flags
& VIDEO_AUDIO_BASS
)
132 ret
|= SOUND_MASK_BASS
;
133 if (va
.flags
& VIDEO_AUDIO_TREBLE
)
134 ret
|= SOUND_MASK_TREBLE
;
137 case MIXER_WRITE(SOUND_MIXER_VOLUME
):
138 left
= mix_to_v4l(val
);
139 right
= mix_to_v4l(val
>> 8);
140 va
.volume
= max(left
,right
);
141 va
.balance
= (32768*min(left
,right
)) / (va
.volume
? va
.volume
: 1);
142 va
.balance
= (left
<right
) ? (65535-va
.balance
) : va
.balance
;
144 va
.flags
&= ~VIDEO_AUDIO_MUTE
;
145 client
->driver
->command(client
,VIDIOCSAUDIO
,&va
);
146 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
148 case MIXER_READ(SOUND_MIXER_VOLUME
):
149 left
= (min(65536 - va
.balance
,32768) *
151 right
= (min(va
.balance
,(u16
)32768) *
153 ret
= v4l_to_mix2(left
,right
);
156 case MIXER_WRITE(SOUND_MIXER_BASS
):
157 va
.bass
= mix_to_v4l(val
);
158 client
->driver
->command(client
,VIDIOCSAUDIO
,&va
);
159 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
161 case MIXER_READ(SOUND_MIXER_BASS
):
162 ret
= v4l_to_mix(va
.bass
);
165 case MIXER_WRITE(SOUND_MIXER_TREBLE
):
166 va
.treble
= mix_to_v4l(val
);
167 client
->driver
->command(client
,VIDIOCSAUDIO
,&va
);
168 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
170 case MIXER_READ(SOUND_MIXER_TREBLE
):
171 ret
= v4l_to_mix(va
.treble
);
177 if (put_user(ret
, p
))
182 static int tvmixer_open(struct inode
*inode
, struct file
*file
)
184 int i
, minor
= iminor(inode
);
185 struct TVMIXER
*mix
= NULL
;
186 struct i2c_client
*client
= NULL
;
188 for (i
= 0; i
< DEV_MAX
; i
++) {
189 if (devices
[i
].minor
== minor
) {
199 /* lock bttv in memory while the mixer is in use */
200 file
->private_data
= mix
;
202 if (client
->adapter
->inc_use
)
203 client
->adapter
->inc_use(client
->adapter
);
205 if (client
->adapter
->owner
)
206 try_module_get(client
->adapter
->owner
);
210 static int tvmixer_release(struct inode
*inode
, struct file
*file
)
212 struct TVMIXER
*mix
= file
->private_data
;
213 struct i2c_client
*client
;
216 if (NULL
== client
) {
221 if (client
->adapter
->dec_use
)
222 client
->adapter
->dec_use(client
->adapter
);
224 if (client
->adapter
->owner
)
225 module_put(client
->adapter
->owner
);
229 static struct i2c_driver driver
= {
232 .owner
= THIS_MODULE
,
233 .name
= "tv card mixer driver",
236 .name
= "tv card mixer driver",
238 .id
= I2C_DRIVERID_TVMIXER
,
239 .detach_adapter
= tvmixer_adapters
,
240 .attach_adapter
= tvmixer_adapters
,
241 .detach_client
= tvmixer_clients
,
244 static struct file_operations tvmixer_fops
= {
245 .owner
= THIS_MODULE
,
247 .ioctl
= tvmixer_ioctl
,
248 .open
= tvmixer_open
,
249 .release
= tvmixer_release
,
252 /* ----------------------------------------------------------------------- */
254 static int tvmixer_adapters(struct i2c_adapter
*adap
)
256 struct list_head
*item
;
257 struct i2c_client
*client
;
259 list_for_each(item
,&adap
->clients
) {
260 client
= list_entry(item
, struct i2c_client
, list
);
261 tvmixer_clients(client
);
266 static int tvmixer_clients(struct i2c_client
*client
)
268 struct video_audio va
;
271 #ifdef I2C_CLASS_TV_ANALOG
272 if (!(client
->adapter
->class & I2C_CLASS_TV_ANALOG
))
276 switch (client
->adapter
->id
) {
277 case I2C_HW_SMBUS_VOODOO3
:
280 /* ok, have a look ... */
283 /* ignore that one */
289 for (i
= 0; i
< DEV_MAX
; i
++) {
290 if (devices
[i
].dev
== client
) {
292 unregister_sound_mixer(devices
[i
].minor
);
293 devices
[i
].dev
= NULL
;
294 devices
[i
].minor
= -1;
295 printk("tvmixer: %s unregistered (#1)\n",
301 /* look for a free slot */
302 for (i
= 0; i
< DEV_MAX
; i
++)
303 if (NULL
== devices
[i
].dev
)
306 printk(KERN_WARNING
"tvmixer: DEV_MAX too small\n");
310 /* audio chip with mixer ??? */
311 if (NULL
== client
->driver
->command
)
313 memset(&va
,0,sizeof(va
));
314 if (0 != client
->driver
->command(client
,VIDIOCGAUDIO
,&va
))
316 if (0 == (va
.flags
& VIDEO_AUDIO_VOLUME
))
319 /* everything is fine, register */
320 if ((minor
= register_sound_mixer(&tvmixer_fops
,devnr
)) < 0) {
321 printk(KERN_ERR
"tvmixer: cannot allocate mixer device\n");
325 devices
[i
].minor
= minor
;
326 devices
[i
].count
= 0;
327 devices
[i
].dev
= client
;
328 printk("tvmixer: %s (%s) registered with minor %d\n",
329 client
->name
,client
->adapter
->name
,minor
);
334 /* ----------------------------------------------------------------------- */
336 static int __init
tvmixer_init_module(void)
340 for (i
= 0; i
< DEV_MAX
; i
++)
341 devices
[i
].minor
= -1;
343 return i2c_add_driver(&driver
);
346 static void __exit
tvmixer_cleanup_module(void)
350 i2c_del_driver(&driver
);
351 for (i
= 0; i
< DEV_MAX
; i
++) {
352 if (devices
[i
].minor
!= -1) {
353 unregister_sound_mixer(devices
[i
].minor
);
354 printk("tvmixer: %s unregistered (#2)\n",
355 devices
[i
].dev
->name
);
360 module_init(tvmixer_init_module
);
361 module_exit(tvmixer_cleanup_module
);
364 * Overrides for Emacs so that we follow Linus's tabbing style.
365 * ---------------------------------------------------------------------------