blob: 9fa5b702e0736ddfc99b26177bae0c547aefd70d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#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>
17
18#include <asm/semaphore.h>
19#include <asm/uaccess.h>
20
21
22#define DEV_MAX 4
23
24static int devnr = -1;
25module_param(devnr, int, 0644);
26
27MODULE_AUTHOR("Gerd Knorr");
28MODULE_LICENSE("GPL");
29
30/* ----------------------------------------------------------------------- */
31
32struct TVMIXER {
33 struct i2c_client *dev;
34 int minor;
35 int count;
36};
37
38static struct TVMIXER devices[DEV_MAX];
39
40static int tvmixer_adapters(struct i2c_adapter *adap);
41static int tvmixer_clients(struct i2c_client *client);
42
43/* ----------------------------------------------------------------------- */
44
45static int mix_to_v4l(int i)
46{
47 int r;
48
49 r = ((i & 0xff) * 65536 + 50) / 100;
50 if (r > 65535) r = 65535;
51 if (r < 0) r = 0;
52 return r;
53}
54
55static int v4l_to_mix(int i)
56{
57 int r;
58
59 r = (i * 100 + 32768) / 65536;
60 if (r > 100) r = 100;
61 if (r < 0) r = 0;
62 return r | (r << 8);
63}
64
65static int v4l_to_mix2(int l, int r)
66{
67 r = (r * 100 + 32768) / 65536;
68 if (r > 100) r = 100;
69 if (r < 0) r = 0;
70 l = (l * 100 + 32768) / 65536;
71 if (l > 100) l = 100;
72 if (l < 0) l = 0;
73 return (r << 8) | l;
74}
75
76static int tvmixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
77{
78 struct video_audio va;
79 int left,right,ret,val = 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080080 struct TVMIXER *mix = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct i2c_client *client = mix->dev;
82 void __user *argp = (void __user *)arg;
83 int __user *p = argp;
84
85 if (NULL == client)
86 return -ENODEV;
87
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080088 if (cmd == SOUND_MIXER_INFO) {
89 mixer_info 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)))
94 return -EFAULT;
95 return 0;
96 }
97 if (cmd == SOUND_OLD_MIXER_INFO) {
98 _old_mixer_info 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)))
102 return -EFAULT;
103 return 0;
104 }
105 if (cmd == OSS_GETVERSION)
106 return put_user(SOUND_VERSION, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
109 if (get_user(val, p))
110 return -EFAULT;
111
112 /* read state */
113 memset(&va,0,sizeof(va));
114 client->driver->command(client,VIDIOCGAUDIO,&va);
115
116 switch (cmd) {
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):
121 ret = 0;
122 break;
123
124 case MIXER_READ(SOUND_MIXER_STEREODEVS):
125 ret = SOUND_MASK_VOLUME;
126 break;
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;
133 break;
134
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;
141 if (va.volume)
142 va.flags &= ~VIDEO_AUDIO_MUTE;
143 client->driver->command(client,VIDIOCSAUDIO,&va);
144 client->driver->command(client,VIDIOCGAUDIO,&va);
145 /* fall throuth */
146 case MIXER_READ(SOUND_MIXER_VOLUME):
147 left = (min(65536 - va.balance,32768) *
148 va.volume) / 32768;
149 right = (min(va.balance,(u16)32768) *
150 va.volume) / 32768;
151 ret = v4l_to_mix2(left,right);
152 break;
153
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);
158 /* fall throuth */
159 case MIXER_READ(SOUND_MIXER_BASS):
160 ret = v4l_to_mix(va.bass);
161 break;
162
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);
167 /* fall throuth */
168 case MIXER_READ(SOUND_MIXER_TREBLE):
169 ret = v4l_to_mix(va.treble);
170 break;
171
172 default:
173 return -EINVAL;
174 }
175 if (put_user(ret, p))
176 return -EFAULT;
177 return 0;
178}
179
180static int tvmixer_open(struct inode *inode, struct file *file)
181{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800182 int i, minor = iminor(inode);
183 struct TVMIXER *mix = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct i2c_client *client = NULL;
185
186 for (i = 0; i < DEV_MAX; i++) {
187 if (devices[i].minor == minor) {
188 mix = devices+i;
189 client = mix->dev;
190 break;
191 }
192 }
193
194 if (NULL == client)
195 return -ENODEV;
196
197 /* lock bttv in memory while the mixer is in use */
198 file->private_data = mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (client->adapter->owner)
200 try_module_get(client->adapter->owner);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204static int tvmixer_release(struct inode *inode, struct file *file)
205{
206 struct TVMIXER *mix = file->private_data;
207 struct i2c_client *client;
208
209 client = mix->dev;
210 if (NULL == client) {
211 return -ENODEV;
212 }
213
Mariusz Kozlowskicededbfc2007-01-07 10:39:44 -0300214 module_put(client->adapter->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 0;
216}
217
218static struct i2c_driver driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100219 .driver = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200220 .name = "tvmixer",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100221 },
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800222 .id = I2C_DRIVERID_TVMIXER,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800223 .detach_adapter = tvmixer_adapters,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800224 .attach_adapter = tvmixer_adapters,
225 .detach_client = tvmixer_clients,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
Arjan van de Venfa027c22007-02-12 00:55:33 -0800228static const struct file_operations tvmixer_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .owner = THIS_MODULE,
230 .llseek = no_llseek,
231 .ioctl = tvmixer_ioctl,
232 .open = tvmixer_open,
233 .release = tvmixer_release,
234};
235
236/* ----------------------------------------------------------------------- */
237
238static int tvmixer_adapters(struct i2c_adapter *adap)
239{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct i2c_client *client;
241
Trent Piephoa991f442007-10-10 05:37:43 -0300242 list_for_each_entry(client, &adap->clients, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 tvmixer_clients(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
246
247static int tvmixer_clients(struct i2c_client *client)
248{
249 struct video_audio va;
250 int i,minor;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!(client->adapter->class & I2C_CLASS_TV_ANALOG))
253 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /* unregister ?? */
256 for (i = 0; i < DEV_MAX; i++) {
257 if (devices[i].dev == client) {
258 /* unregister */
259 unregister_sound_mixer(devices[i].minor);
260 devices[i].dev = NULL;
261 devices[i].minor = -1;
262 printk("tvmixer: %s unregistered (#1)\n",
Jean Delvarefae91e72005-08-15 19:57:04 +0200263 client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return 0;
265 }
266 }
267
268 /* look for a free slot */
269 for (i = 0; i < DEV_MAX; i++)
270 if (NULL == devices[i].dev)
271 break;
272 if (i == DEV_MAX) {
273 printk(KERN_WARNING "tvmixer: DEV_MAX too small\n");
274 return -1;
275 }
276
277 /* audio chip with mixer ??? */
278 if (NULL == client->driver->command)
279 return -1;
280 memset(&va,0,sizeof(va));
281 if (0 != client->driver->command(client,VIDIOCGAUDIO,&va))
282 return -1;
283 if (0 == (va.flags & VIDEO_AUDIO_VOLUME))
284 return -1;
285
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");
289 return -1;
290 }
291
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);
297
298 return 0;
299}
300
301/* ----------------------------------------------------------------------- */
302
303static int __init tvmixer_init_module(void)
304{
305 int i;
306
307 for (i = 0; i < DEV_MAX; i++)
308 devices[i].minor = -1;
309
310 return i2c_add_driver(&driver);
311}
312
313static void __exit tvmixer_cleanup_module(void)
314{
315 int i;
316
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",
Jean Delvarefae91e72005-08-15 19:57:04 +0200322 devices[i].dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 }
325}
326
327module_init(tvmixer_init_module);
328module_exit(tvmixer_cleanup_module);
329
330/*
331 * Overrides for Emacs so that we follow Linus's tabbing style.
332 * ---------------------------------------------------------------------------
333 * Local variables:
334 * c-basic-offset: 8
335 * End:
336 */