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