4 #include <linux/module.h>
5 #include <linux/kernel.h>
6 #include <linux/string.h>
7 #include <linux/timer.h>
8 #include <linux/delay.h>
9 #include <linux/errno.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/videodev.h>
13 #include <linux/init.h>
14 #include <linux/kdev_t.h>
15 #include <linux/sound.h>
16 #include <linux/soundcard.h>
18 #include <asm/semaphore.h>
19 #include <asm/uaccess.h>
24 static int devnr
= -1;
25 module_param(devnr
, int, 0644);
27 MODULE_AUTHOR("Gerd Knorr");
28 MODULE_LICENSE("GPL");
30 /* ----------------------------------------------------------------------- */
33 struct i2c_client
*dev
;
38 static struct TVMIXER devices
[DEV_MAX
];
40 static int tvmixer_adapters(struct i2c_adapter
*adap
);
41 static int tvmixer_clients(struct i2c_client
*client
);
43 /* ----------------------------------------------------------------------- */
45 static int mix_to_v4l(int i
)
49 r
= ((i
& 0xff) * 65536 + 50) / 100;
50 if (r
> 65535) r
= 65535;
55 static int v4l_to_mix(int i
)
59 r
= (i
* 100 + 32768) / 65536;
65 static int v4l_to_mix2(int l
, int r
)
67 r
= (r
* 100 + 32768) / 65536;
70 l
= (l
* 100 + 32768) / 65536;
76 static int tvmixer_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
78 struct video_audio va
;
79 int left
,right
,ret
,val
= 0;
80 struct TVMIXER
*mix
= file
->private_data
;
81 struct i2c_client
*client
= mix
->dev
;
82 void __user
*argp
= (void __user
*)arg
;
88 if (cmd
== SOUND_MIXER_INFO
) {
90 strlcpy(info
.id
, "tv card", sizeof(info
.id
));
91 strlcpy(info
.name
, client
->name
, sizeof(info
.name
));
92 info
.modify_counter
= 42 /* FIXME */;
93 if (copy_to_user(argp
, &info
, sizeof(info
)))
97 if (cmd
== SOUND_OLD_MIXER_INFO
) {
99 strlcpy(info
.id
, "tv card", sizeof(info
.id
));
100 strlcpy(info
.name
, client
->name
, sizeof(info
.name
));
101 if (copy_to_user(argp
, &info
, sizeof(info
)))
105 if (cmd
== OSS_GETVERSION
)
106 return put_user(SOUND_VERSION
, p
);
108 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
109 if (get_user(val
, p
))
113 memset(&va
,0,sizeof(va
));
114 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
117 case MIXER_READ(SOUND_MIXER_RECMASK
):
118 case MIXER_READ(SOUND_MIXER_CAPS
):
119 case MIXER_READ(SOUND_MIXER_RECSRC
):
120 case MIXER_WRITE(SOUND_MIXER_RECSRC
):
124 case MIXER_READ(SOUND_MIXER_STEREODEVS
):
125 ret
= SOUND_MASK_VOLUME
;
127 case MIXER_READ(SOUND_MIXER_DEVMASK
):
128 ret
= SOUND_MASK_VOLUME
;
129 if (va
.flags
& VIDEO_AUDIO_BASS
)
130 ret
|= SOUND_MASK_BASS
;
131 if (va
.flags
& VIDEO_AUDIO_TREBLE
)
132 ret
|= SOUND_MASK_TREBLE
;
135 case MIXER_WRITE(SOUND_MIXER_VOLUME
):
136 left
= mix_to_v4l(val
);
137 right
= mix_to_v4l(val
>> 8);
138 va
.volume
= max(left
,right
);
139 va
.balance
= (32768*min(left
,right
)) / (va
.volume
? va
.volume
: 1);
140 va
.balance
= (left
<right
) ? (65535-va
.balance
) : va
.balance
;
142 va
.flags
&= ~VIDEO_AUDIO_MUTE
;
143 client
->driver
->command(client
,VIDIOCSAUDIO
,&va
);
144 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
146 case MIXER_READ(SOUND_MIXER_VOLUME
):
147 left
= (min(65536 - va
.balance
,32768) *
149 right
= (min(va
.balance
,(u16
)32768) *
151 ret
= v4l_to_mix2(left
,right
);
154 case MIXER_WRITE(SOUND_MIXER_BASS
):
155 va
.bass
= mix_to_v4l(val
);
156 client
->driver
->command(client
,VIDIOCSAUDIO
,&va
);
157 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
159 case MIXER_READ(SOUND_MIXER_BASS
):
160 ret
= v4l_to_mix(va
.bass
);
163 case MIXER_WRITE(SOUND_MIXER_TREBLE
):
164 va
.treble
= mix_to_v4l(val
);
165 client
->driver
->command(client
,VIDIOCSAUDIO
,&va
);
166 client
->driver
->command(client
,VIDIOCGAUDIO
,&va
);
168 case MIXER_READ(SOUND_MIXER_TREBLE
):
169 ret
= v4l_to_mix(va
.treble
);
175 if (put_user(ret
, p
))
180 static int tvmixer_open(struct inode
*inode
, struct file
*file
)
182 int i
, minor
= iminor(inode
);
183 struct TVMIXER
*mix
= NULL
;
184 struct i2c_client
*client
= NULL
;
186 for (i
= 0; i
< DEV_MAX
; i
++) {
187 if (devices
[i
].minor
== minor
) {
197 /* lock bttv in memory while the mixer is in use */
198 file
->private_data
= mix
;
199 if (client
->adapter
->owner
)
200 try_module_get(client
->adapter
->owner
);
204 static int tvmixer_release(struct inode
*inode
, struct file
*file
)
206 struct TVMIXER
*mix
= file
->private_data
;
207 struct i2c_client
*client
;
210 if (NULL
== client
) {
214 module_put(client
->adapter
->owner
);
218 static struct i2c_driver driver
= {
222 .id
= I2C_DRIVERID_TVMIXER
,
223 .detach_adapter
= tvmixer_adapters
,
224 .attach_adapter
= tvmixer_adapters
,
225 .detach_client
= tvmixer_clients
,
228 static const struct file_operations tvmixer_fops
= {
229 .owner
= THIS_MODULE
,
231 .ioctl
= tvmixer_ioctl
,
232 .open
= tvmixer_open
,
233 .release
= tvmixer_release
,
236 /* ----------------------------------------------------------------------- */
238 static int tvmixer_adapters(struct i2c_adapter
*adap
)
240 struct i2c_client
*client
;
242 list_for_each_entry(client
, &adap
->clients
, list
)
243 tvmixer_clients(client
);
247 static int tvmixer_clients(struct i2c_client
*client
)
249 struct video_audio va
;
252 if (!(client
->adapter
->class & I2C_CLASS_TV_ANALOG
))
256 for (i
= 0; i
< DEV_MAX
; i
++) {
257 if (devices
[i
].dev
== client
) {
259 unregister_sound_mixer(devices
[i
].minor
);
260 devices
[i
].dev
= NULL
;
261 devices
[i
].minor
= -1;
262 printk("tvmixer: %s unregistered (#1)\n",
268 /* look for a free slot */
269 for (i
= 0; i
< DEV_MAX
; i
++)
270 if (NULL
== devices
[i
].dev
)
273 printk(KERN_WARNING
"tvmixer: DEV_MAX too small\n");
277 /* audio chip with mixer ??? */
278 if (NULL
== client
->driver
->command
)
280 memset(&va
,0,sizeof(va
));
281 if (0 != client
->driver
->command(client
,VIDIOCGAUDIO
,&va
))
283 if (0 == (va
.flags
& VIDEO_AUDIO_VOLUME
))
286 /* everything is fine, register */
287 if ((minor
= register_sound_mixer(&tvmixer_fops
,devnr
)) < 0) {
288 printk(KERN_ERR
"tvmixer: cannot allocate mixer device\n");
292 devices
[i
].minor
= minor
;
293 devices
[i
].count
= 0;
294 devices
[i
].dev
= client
;
295 printk("tvmixer: %s (%s) registered with minor %d\n",
296 client
->name
,client
->adapter
->name
,minor
);
301 /* ----------------------------------------------------------------------- */
303 static int __init
tvmixer_init_module(void)
307 for (i
= 0; i
< DEV_MAX
; i
++)
308 devices
[i
].minor
= -1;
310 return i2c_add_driver(&driver
);
313 static void __exit
tvmixer_cleanup_module(void)
317 i2c_del_driver(&driver
);
318 for (i
= 0; i
< DEV_MAX
; i
++) {
319 if (devices
[i
].minor
!= -1) {
320 unregister_sound_mixer(devices
[i
].minor
);
321 printk("tvmixer: %s unregistered (#2)\n",
322 devices
[i
].dev
->name
);
327 module_init(tvmixer_init_module
);
328 module_exit(tvmixer_cleanup_module
);
331 * Overrides for Emacs so that we follow Linus's tabbing style.
332 * ---------------------------------------------------------------------------