]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #include <linux/module.h> |
2 | #include <linux/moduleparam.h> | |
3 | #include <linux/kernel.h> | |
4 | #include <linux/sched.h> | |
5 | #include <linux/string.h> | |
6 | #include <linux/timer.h> | |
7 | #include <linux/delay.h> | |
8 | #include <linux/errno.h> | |
9 | #include <linux/slab.h> | |
10 | #include <linux/i2c.h> | |
11 | #include <linux/videodev.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/kdev_t.h> | |
14 | #include <linux/sound.h> | |
15 | #include <linux/soundcard.h> | |
16 | ||
17 | #include <asm/semaphore.h> | |
18 | #include <asm/uaccess.h> | |
19 | ||
20 | ||
21 | #define DEV_MAX 4 | |
22 | ||
23 | static int devnr = -1; | |
24 | module_param(devnr, int, 0644); | |
25 | ||
26 | MODULE_AUTHOR("Gerd Knorr"); | |
27 | MODULE_LICENSE("GPL"); | |
28 | ||
29 | /* ----------------------------------------------------------------------- */ | |
30 | ||
31 | struct TVMIXER { | |
32 | struct i2c_client *dev; | |
33 | int minor; | |
34 | int count; | |
35 | }; | |
36 | ||
37 | static struct TVMIXER devices[DEV_MAX]; | |
38 | ||
39 | static int tvmixer_adapters(struct i2c_adapter *adap); | |
40 | static int tvmixer_clients(struct i2c_client *client); | |
41 | ||
42 | /* ----------------------------------------------------------------------- */ | |
43 | ||
44 | static int mix_to_v4l(int i) | |
45 | { | |
46 | int r; | |
47 | ||
48 | r = ((i & 0xff) * 65536 + 50) / 100; | |
49 | if (r > 65535) r = 65535; | |
50 | if (r < 0) r = 0; | |
51 | return r; | |
52 | } | |
53 | ||
54 | static int v4l_to_mix(int i) | |
55 | { | |
56 | int r; | |
57 | ||
58 | r = (i * 100 + 32768) / 65536; | |
59 | if (r > 100) r = 100; | |
60 | if (r < 0) r = 0; | |
61 | return r | (r << 8); | |
62 | } | |
63 | ||
64 | static int v4l_to_mix2(int l, int r) | |
65 | { | |
66 | r = (r * 100 + 32768) / 65536; | |
67 | if (r > 100) r = 100; | |
68 | if (r < 0) r = 0; | |
69 | l = (l * 100 + 32768) / 65536; | |
70 | if (l > 100) l = 100; | |
71 | if (l < 0) l = 0; | |
72 | return (r << 8) | l; | |
73 | } | |
74 | ||
75 | static int tvmixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) | |
76 | { | |
77 | struct video_audio va; | |
78 | int left,right,ret,val = 0; | |
79 | struct TVMIXER *mix = file->private_data; | |
80 | struct i2c_client *client = mix->dev; | |
81 | void __user *argp = (void __user *)arg; | |
82 | int __user *p = argp; | |
83 | ||
84 | if (NULL == client) | |
85 | return -ENODEV; | |
86 | ||
87 | if (cmd == SOUND_MIXER_INFO) { | |
88 | mixer_info info; | |
89 | strlcpy(info.id, "tv card", sizeof(info.id)); | |
90 | strlcpy(info.name, i2c_clientname(client), sizeof(info.name)); | |
91 | info.modify_counter = 42 /* FIXME */; | |
92 | if (copy_to_user(argp, &info, sizeof(info))) | |
93 | return -EFAULT; | |
94 | return 0; | |
95 | } | |
96 | if (cmd == SOUND_OLD_MIXER_INFO) { | |
97 | _old_mixer_info info; | |
98 | strlcpy(info.id, "tv card", sizeof(info.id)); | |
99 | strlcpy(info.name, i2c_clientname(client), sizeof(info.name)); | |
100 | if (copy_to_user(argp, &info, sizeof(info))) | |
101 | return -EFAULT; | |
102 | return 0; | |
103 | } | |
104 | if (cmd == OSS_GETVERSION) | |
105 | return put_user(SOUND_VERSION, p); | |
106 | ||
107 | if (_SIOC_DIR(cmd) & _SIOC_WRITE) | |
108 | if (get_user(val, p)) | |
109 | return -EFAULT; | |
110 | ||
111 | /* read state */ | |
112 | memset(&va,0,sizeof(va)); | |
113 | client->driver->command(client,VIDIOCGAUDIO,&va); | |
114 | ||
115 | switch (cmd) { | |
116 | case MIXER_READ(SOUND_MIXER_RECMASK): | |
117 | case MIXER_READ(SOUND_MIXER_CAPS): | |
118 | case MIXER_READ(SOUND_MIXER_RECSRC): | |
119 | case MIXER_WRITE(SOUND_MIXER_RECSRC): | |
120 | ret = 0; | |
121 | break; | |
122 | ||
123 | case MIXER_READ(SOUND_MIXER_STEREODEVS): | |
124 | ret = SOUND_MASK_VOLUME; | |
125 | break; | |
126 | case MIXER_READ(SOUND_MIXER_DEVMASK): | |
127 | ret = SOUND_MASK_VOLUME; | |
128 | if (va.flags & VIDEO_AUDIO_BASS) | |
129 | ret |= SOUND_MASK_BASS; | |
130 | if (va.flags & VIDEO_AUDIO_TREBLE) | |
131 | ret |= SOUND_MASK_TREBLE; | |
132 | break; | |
133 | ||
134 | case MIXER_WRITE(SOUND_MIXER_VOLUME): | |
135 | left = mix_to_v4l(val); | |
136 | right = mix_to_v4l(val >> 8); | |
137 | va.volume = max(left,right); | |
138 | va.balance = (32768*min(left,right)) / (va.volume ? va.volume : 1); | |
139 | va.balance = (left<right) ? (65535-va.balance) : va.balance; | |
140 | if (va.volume) | |
141 | va.flags &= ~VIDEO_AUDIO_MUTE; | |
142 | client->driver->command(client,VIDIOCSAUDIO,&va); | |
143 | client->driver->command(client,VIDIOCGAUDIO,&va); | |
144 | /* fall throuth */ | |
145 | case MIXER_READ(SOUND_MIXER_VOLUME): | |
146 | left = (min(65536 - va.balance,32768) * | |
147 | va.volume) / 32768; | |
148 | right = (min(va.balance,(u16)32768) * | |
149 | va.volume) / 32768; | |
150 | ret = v4l_to_mix2(left,right); | |
151 | break; | |
152 | ||
153 | case MIXER_WRITE(SOUND_MIXER_BASS): | |
154 | va.bass = mix_to_v4l(val); | |
155 | client->driver->command(client,VIDIOCSAUDIO,&va); | |
156 | client->driver->command(client,VIDIOCGAUDIO,&va); | |
157 | /* fall throuth */ | |
158 | case MIXER_READ(SOUND_MIXER_BASS): | |
159 | ret = v4l_to_mix(va.bass); | |
160 | break; | |
161 | ||
162 | case MIXER_WRITE(SOUND_MIXER_TREBLE): | |
163 | va.treble = mix_to_v4l(val); | |
164 | client->driver->command(client,VIDIOCSAUDIO,&va); | |
165 | client->driver->command(client,VIDIOCGAUDIO,&va); | |
166 | /* fall throuth */ | |
167 | case MIXER_READ(SOUND_MIXER_TREBLE): | |
168 | ret = v4l_to_mix(va.treble); | |
169 | break; | |
170 | ||
171 | default: | |
172 | return -EINVAL; | |
173 | } | |
174 | if (put_user(ret, p)) | |
175 | return -EFAULT; | |
176 | return 0; | |
177 | } | |
178 | ||
179 | static int tvmixer_open(struct inode *inode, struct file *file) | |
180 | { | |
181 | int i, minor = iminor(inode); | |
182 | struct TVMIXER *mix = NULL; | |
183 | struct i2c_client *client = NULL; | |
184 | ||
185 | for (i = 0; i < DEV_MAX; i++) { | |
186 | if (devices[i].minor == minor) { | |
187 | mix = devices+i; | |
188 | client = mix->dev; | |
189 | break; | |
190 | } | |
191 | } | |
192 | ||
193 | if (NULL == client) | |
194 | return -ENODEV; | |
195 | ||
196 | /* lock bttv in memory while the mixer is in use */ | |
197 | file->private_data = mix; | |
198 | #ifndef I2C_PEC | |
199 | if (client->adapter->inc_use) | |
200 | client->adapter->inc_use(client->adapter); | |
201 | #endif | |
202 | if (client->adapter->owner) | |
203 | try_module_get(client->adapter->owner); | |
204 | return 0; | |
205 | } | |
206 | ||
207 | static int tvmixer_release(struct inode *inode, struct file *file) | |
208 | { | |
209 | struct TVMIXER *mix = file->private_data; | |
210 | struct i2c_client *client; | |
211 | ||
212 | client = mix->dev; | |
213 | if (NULL == client) { | |
214 | return -ENODEV; | |
215 | } | |
216 | ||
217 | #ifndef I2C_PEC | |
218 | if (client->adapter->dec_use) | |
219 | client->adapter->dec_use(client->adapter); | |
220 | #endif | |
221 | if (client->adapter->owner) | |
222 | module_put(client->adapter->owner); | |
223 | return 0; | |
224 | } | |
225 | ||
226 | static struct i2c_driver driver = { | |
227 | #ifdef I2C_PEC | |
228 | .owner = THIS_MODULE, | |
229 | #endif | |
230 | .name = "tv card mixer driver", | |
231 | .id = I2C_DRIVERID_TVMIXER, | |
232 | #ifdef I2C_DF_DUMMY | |
233 | .flags = I2C_DF_DUMMY, | |
234 | #else | |
235 | .flags = I2C_DF_NOTIFY, | |
236 | .detach_adapter = tvmixer_adapters, | |
237 | #endif | |
238 | .attach_adapter = tvmixer_adapters, | |
239 | .detach_client = tvmixer_clients, | |
240 | }; | |
241 | ||
242 | static struct file_operations tvmixer_fops = { | |
243 | .owner = THIS_MODULE, | |
244 | .llseek = no_llseek, | |
245 | .ioctl = tvmixer_ioctl, | |
246 | .open = tvmixer_open, | |
247 | .release = tvmixer_release, | |
248 | }; | |
249 | ||
250 | /* ----------------------------------------------------------------------- */ | |
251 | ||
252 | static int tvmixer_adapters(struct i2c_adapter *adap) | |
253 | { | |
254 | struct list_head *item; | |
255 | struct i2c_client *client; | |
256 | ||
257 | list_for_each(item,&adap->clients) { | |
258 | client = list_entry(item, struct i2c_client, list); | |
259 | tvmixer_clients(client); | |
260 | } | |
261 | return 0; | |
262 | } | |
263 | ||
264 | static int tvmixer_clients(struct i2c_client *client) | |
265 | { | |
266 | struct video_audio va; | |
267 | int i,minor; | |
268 | ||
269 | #ifdef I2C_CLASS_TV_ANALOG | |
270 | if (!(client->adapter->class & I2C_CLASS_TV_ANALOG)) | |
271 | return -1; | |
272 | #else | |
273 | /* TV card ??? */ | |
274 | switch (client->adapter->id) { | |
275 | case I2C_ALGO_BIT | I2C_HW_SMBUS_VOODOO3: | |
276 | case I2C_ALGO_BIT | I2C_HW_B_BT848: | |
277 | case I2C_ALGO_BIT | I2C_HW_B_RIVA: | |
278 | /* ok, have a look ... */ | |
279 | break; | |
280 | default: | |
281 | /* ignore that one */ | |
282 | return -1; | |
283 | } | |
284 | #endif | |
285 | ||
286 | /* unregister ?? */ | |
287 | for (i = 0; i < DEV_MAX; i++) { | |
288 | if (devices[i].dev == client) { | |
289 | /* unregister */ | |
290 | unregister_sound_mixer(devices[i].minor); | |
291 | devices[i].dev = NULL; | |
292 | devices[i].minor = -1; | |
293 | printk("tvmixer: %s unregistered (#1)\n", | |
294 | i2c_clientname(client)); | |
295 | return 0; | |
296 | } | |
297 | } | |
298 | ||
299 | /* look for a free slot */ | |
300 | for (i = 0; i < DEV_MAX; i++) | |
301 | if (NULL == devices[i].dev) | |
302 | break; | |
303 | if (i == DEV_MAX) { | |
304 | printk(KERN_WARNING "tvmixer: DEV_MAX too small\n"); | |
305 | return -1; | |
306 | } | |
307 | ||
308 | /* audio chip with mixer ??? */ | |
309 | if (NULL == client->driver->command) | |
310 | return -1; | |
311 | memset(&va,0,sizeof(va)); | |
312 | if (0 != client->driver->command(client,VIDIOCGAUDIO,&va)) | |
313 | return -1; | |
314 | if (0 == (va.flags & VIDEO_AUDIO_VOLUME)) | |
315 | return -1; | |
316 | ||
317 | /* everything is fine, register */ | |
318 | if ((minor = register_sound_mixer(&tvmixer_fops,devnr)) < 0) { | |
319 | printk(KERN_ERR "tvmixer: cannot allocate mixer device\n"); | |
320 | return -1; | |
321 | } | |
322 | ||
323 | devices[i].minor = minor; | |
324 | devices[i].count = 0; | |
325 | devices[i].dev = client; | |
326 | printk("tvmixer: %s (%s) registered with minor %d\n", | |
327 | client->name,client->adapter->name,minor); | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
332 | /* ----------------------------------------------------------------------- */ | |
333 | ||
334 | static int __init tvmixer_init_module(void) | |
335 | { | |
336 | int i; | |
337 | ||
338 | for (i = 0; i < DEV_MAX; i++) | |
339 | devices[i].minor = -1; | |
340 | ||
341 | return i2c_add_driver(&driver); | |
342 | } | |
343 | ||
344 | static void __exit tvmixer_cleanup_module(void) | |
345 | { | |
346 | int i; | |
347 | ||
348 | i2c_del_driver(&driver); | |
349 | for (i = 0; i < DEV_MAX; i++) { | |
350 | if (devices[i].minor != -1) { | |
351 | unregister_sound_mixer(devices[i].minor); | |
352 | printk("tvmixer: %s unregistered (#2)\n", | |
353 | i2c_clientname(devices[i].dev)); | |
354 | } | |
355 | } | |
356 | } | |
357 | ||
358 | module_init(tvmixer_init_module); | |
359 | module_exit(tvmixer_cleanup_module); | |
360 | ||
361 | /* | |
362 | * Overrides for Emacs so that we follow Linus's tabbing style. | |
363 | * --------------------------------------------------------------------------- | |
364 | * Local variables: | |
365 | * c-basic-offset: 8 | |
366 | * End: | |
367 | */ |