blob: 9e86caeb96a7b530da89b05e18a0cb44d53d7865 [file] [log] [blame]
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -07001/*
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -07002 */
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#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>
19
20#include <asm/semaphore.h>
21#include <asm/uaccess.h>
22
23
24#define DEV_MAX 4
25
26static int devnr = -1;
27module_param(devnr, int, 0644);
28
29MODULE_AUTHOR("Gerd Knorr");
30MODULE_LICENSE("GPL");
31
32/* ----------------------------------------------------------------------- */
33
34struct TVMIXER {
35 struct i2c_client *dev;
36 int minor;
37 int count;
38};
39
40static struct TVMIXER devices[DEV_MAX];
41
42static int tvmixer_adapters(struct i2c_adapter *adap);
43static int tvmixer_clients(struct i2c_client *client);
44
45/* ----------------------------------------------------------------------- */
46
47static int mix_to_v4l(int i)
48{
49 int r;
50
51 r = ((i & 0xff) * 65536 + 50) / 100;
52 if (r > 65535) r = 65535;
53 if (r < 0) r = 0;
54 return r;
55}
56
57static int v4l_to_mix(int i)
58{
59 int r;
60
61 r = (i * 100 + 32768) / 65536;
62 if (r > 100) r = 100;
63 if (r < 0) r = 0;
64 return r | (r << 8);
65}
66
67static int v4l_to_mix2(int l, int r)
68{
69 r = (r * 100 + 32768) / 65536;
70 if (r > 100) r = 100;
71 if (r < 0) r = 0;
72 l = (l * 100 + 32768) / 65536;
73 if (l > 100) l = 100;
74 if (l < 0) l = 0;
75 return (r << 8) | l;
76}
77
78static int tvmixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
79{
80 struct video_audio va;
81 int left,right,ret,val = 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080082 struct TVMIXER *mix = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct i2c_client *client = mix->dev;
84 void __user *argp = (void __user *)arg;
85 int __user *p = argp;
86
87 if (NULL == client)
88 return -ENODEV;
89
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080090 if (cmd == SOUND_MIXER_INFO) {
91 mixer_info 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)))
96 return -EFAULT;
97 return 0;
98 }
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)))
104 return -EFAULT;
105 return 0;
106 }
107 if (cmd == OSS_GETVERSION)
108 return put_user(SOUND_VERSION, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
111 if (get_user(val, p))
112 return -EFAULT;
113
114 /* read state */
115 memset(&va,0,sizeof(va));
116 client->driver->command(client,VIDIOCGAUDIO,&va);
117
118 switch (cmd) {
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):
123 ret = 0;
124 break;
125
126 case MIXER_READ(SOUND_MIXER_STEREODEVS):
127 ret = SOUND_MASK_VOLUME;
128 break;
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;
135 break;
136
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;
143 if (va.volume)
144 va.flags &= ~VIDEO_AUDIO_MUTE;
145 client->driver->command(client,VIDIOCSAUDIO,&va);
146 client->driver->command(client,VIDIOCGAUDIO,&va);
147 /* fall throuth */
148 case MIXER_READ(SOUND_MIXER_VOLUME):
149 left = (min(65536 - va.balance,32768) *
150 va.volume) / 32768;
151 right = (min(va.balance,(u16)32768) *
152 va.volume) / 32768;
153 ret = v4l_to_mix2(left,right);
154 break;
155
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);
160 /* fall throuth */
161 case MIXER_READ(SOUND_MIXER_BASS):
162 ret = v4l_to_mix(va.bass);
163 break;
164
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);
169 /* fall throuth */
170 case MIXER_READ(SOUND_MIXER_TREBLE):
171 ret = v4l_to_mix(va.treble);
172 break;
173
174 default:
175 return -EINVAL;
176 }
177 if (put_user(ret, p))
178 return -EFAULT;
179 return 0;
180}
181
182static int tvmixer_open(struct inode *inode, struct file *file)
183{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800184 int i, minor = iminor(inode);
185 struct TVMIXER *mix = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct i2c_client *client = NULL;
187
188 for (i = 0; i < DEV_MAX; i++) {
189 if (devices[i].minor == minor) {
190 mix = devices+i;
191 client = mix->dev;
192 break;
193 }
194 }
195
196 if (NULL == client)
197 return -ENODEV;
198
199 /* lock bttv in memory while the mixer is in use */
200 file->private_data = mix;
201#ifndef I2C_PEC
202 if (client->adapter->inc_use)
203 client->adapter->inc_use(client->adapter);
204#endif
205 if (client->adapter->owner)
206 try_module_get(client->adapter->owner);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210static int tvmixer_release(struct inode *inode, struct file *file)
211{
212 struct TVMIXER *mix = file->private_data;
213 struct i2c_client *client;
214
215 client = mix->dev;
216 if (NULL == client) {
217 return -ENODEV;
218 }
219
220#ifndef I2C_PEC
221 if (client->adapter->dec_use)
222 client->adapter->dec_use(client->adapter);
223#endif
224 if (client->adapter->owner)
225 module_put(client->adapter->owner);
226 return 0;
227}
228
229static struct i2c_driver driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100230 .driver = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200231 .name = "tvmixer",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100232 },
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800233 .id = I2C_DRIVERID_TVMIXER,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800234 .detach_adapter = tvmixer_adapters,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800235 .attach_adapter = tvmixer_adapters,
236 .detach_client = tvmixer_clients,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
239static struct file_operations tvmixer_fops = {
240 .owner = THIS_MODULE,
241 .llseek = no_llseek,
242 .ioctl = tvmixer_ioctl,
243 .open = tvmixer_open,
244 .release = tvmixer_release,
245};
246
247/* ----------------------------------------------------------------------- */
248
249static int tvmixer_adapters(struct i2c_adapter *adap)
250{
251 struct list_head *item;
252 struct i2c_client *client;
253
254 list_for_each(item,&adap->clients) {
255 client = list_entry(item, struct i2c_client, list);
256 tvmixer_clients(client);
257 }
258 return 0;
259}
260
261static int tvmixer_clients(struct i2c_client *client)
262{
263 struct video_audio va;
264 int i,minor;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!(client->adapter->class & I2C_CLASS_TV_ANALOG))
267 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* unregister ?? */
270 for (i = 0; i < DEV_MAX; i++) {
271 if (devices[i].dev == client) {
272 /* unregister */
273 unregister_sound_mixer(devices[i].minor);
274 devices[i].dev = NULL;
275 devices[i].minor = -1;
276 printk("tvmixer: %s unregistered (#1)\n",
Jean Delvarefae91e72005-08-15 19:57:04 +0200277 client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279 }
280 }
281
282 /* look for a free slot */
283 for (i = 0; i < DEV_MAX; i++)
284 if (NULL == devices[i].dev)
285 break;
286 if (i == DEV_MAX) {
287 printk(KERN_WARNING "tvmixer: DEV_MAX too small\n");
288 return -1;
289 }
290
291 /* audio chip with mixer ??? */
292 if (NULL == client->driver->command)
293 return -1;
294 memset(&va,0,sizeof(va));
295 if (0 != client->driver->command(client,VIDIOCGAUDIO,&va))
296 return -1;
297 if (0 == (va.flags & VIDEO_AUDIO_VOLUME))
298 return -1;
299
300 /* everything is fine, register */
301 if ((minor = register_sound_mixer(&tvmixer_fops,devnr)) < 0) {
302 printk(KERN_ERR "tvmixer: cannot allocate mixer device\n");
303 return -1;
304 }
305
306 devices[i].minor = minor;
307 devices[i].count = 0;
308 devices[i].dev = client;
309 printk("tvmixer: %s (%s) registered with minor %d\n",
310 client->name,client->adapter->name,minor);
311
312 return 0;
313}
314
315/* ----------------------------------------------------------------------- */
316
317static int __init tvmixer_init_module(void)
318{
319 int i;
320
321 for (i = 0; i < DEV_MAX; i++)
322 devices[i].minor = -1;
323
324 return i2c_add_driver(&driver);
325}
326
327static void __exit tvmixer_cleanup_module(void)
328{
329 int i;
330
331 i2c_del_driver(&driver);
332 for (i = 0; i < DEV_MAX; i++) {
333 if (devices[i].minor != -1) {
334 unregister_sound_mixer(devices[i].minor);
335 printk("tvmixer: %s unregistered (#2)\n",
Jean Delvarefae91e72005-08-15 19:57:04 +0200336 devices[i].dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 }
339}
340
341module_init(tvmixer_init_module);
342module_exit(tvmixer_cleanup_module);
343
344/*
345 * Overrides for Emacs so that we follow Linus's tabbing style.
346 * ---------------------------------------------------------------------------
347 * Local variables:
348 * c-basic-offset: 8
349 * End:
350 */