1 /* 2 * Linux driver for TerraTec DMX 6Fire USB 3 * 4 * Mixer control 5 * 6 * Author: Torsten Schenk <torsten.schenk@zoho.com> 7 * Created: Jan 01, 2011 8 * Copyright: (C) Torsten Schenk 9 * 10 * Thanks to: 11 * - Holger Ruckdeschel: he found out how to control individual channel 12 * volumes and introduced mute switch 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation; either version 2 of the License, or 17 * (at your option) any later version. 18 */ 19 20 #include <linux/interrupt.h> 21 #include <sound/control.h> 22 #include <sound/tlv.h> 23 24 #include "control.h" 25 #include "comm.h" 26 #include "chip.h" 27 28 static char *opt_coax_texts[2] = { "Optical", "Coax" }; 29 static char *line_phono_texts[2] = { "Line", "Phono" }; 30 31 /* 32 * data that needs to be sent to device. sets up card internal stuff. 33 * values dumped from windows driver and filtered by trial'n'error. 34 */ 35 static const struct { 36 u8 type; 37 u8 reg; 38 u8 value; 39 } 40 init_data[] = { 41 { 0x22, 0x00, 0x00 }, { 0x20, 0x00, 0x08 }, { 0x22, 0x01, 0x01 }, 42 { 0x20, 0x01, 0x08 }, { 0x22, 0x02, 0x00 }, { 0x20, 0x02, 0x08 }, 43 { 0x22, 0x03, 0x00 }, { 0x20, 0x03, 0x08 }, { 0x22, 0x04, 0x00 }, 44 { 0x20, 0x04, 0x08 }, { 0x22, 0x05, 0x01 }, { 0x20, 0x05, 0x08 }, 45 { 0x22, 0x04, 0x01 }, { 0x12, 0x04, 0x00 }, { 0x12, 0x05, 0x00 }, 46 { 0x12, 0x0d, 0x38 }, { 0x12, 0x21, 0x82 }, { 0x12, 0x22, 0x80 }, 47 { 0x12, 0x23, 0x00 }, { 0x12, 0x06, 0x02 }, { 0x12, 0x03, 0x00 }, 48 { 0x12, 0x02, 0x00 }, { 0x22, 0x03, 0x01 }, 49 { 0 } /* TERMINATING ENTRY */ 50 }; 51 52 static const int rates_altsetting[] = { 1, 1, 2, 2, 3, 3 }; 53 /* values to write to soundcard register for all samplerates */ 54 static const u16 rates_6fire_vl[] = {0x00, 0x01, 0x00, 0x01, 0x00, 0x01}; 55 static const u16 rates_6fire_vh[] = {0x11, 0x11, 0x10, 0x10, 0x00, 0x00}; 56 57 static DECLARE_TLV_DB_MINMAX(tlv_output, -9000, 0); 58 59 enum { 60 DIGITAL_THRU_ONLY_SAMPLERATE = 3 61 }; 62 63 static void usb6fire_control_output_vol_update(struct control_runtime *rt) 64 { 65 struct comm_runtime *comm_rt = rt->chip->comm; 66 int i; 67 68 if (comm_rt) 69 for (i = 0; i < 6; i++) 70 if (!(rt->ovol_updated & (1 << i))) { 71 comm_rt->write8(comm_rt, 0x12, 0x0f + i, 72 180 - rt->output_vol[i]); 73 rt->ovol_updated |= 1 << i; 74 } 75 } 76 77 static void usb6fire_control_line_phono_update(struct control_runtime *rt) 78 { 79 struct comm_runtime *comm_rt = rt->chip->comm; 80 if (comm_rt) { 81 comm_rt->write8(comm_rt, 0x22, 0x02, rt->line_phono_switch); 82 comm_rt->write8(comm_rt, 0x21, 0x02, rt->line_phono_switch); 83 } 84 } 85 86 static void usb6fire_control_opt_coax_update(struct control_runtime *rt) 87 { 88 struct comm_runtime *comm_rt = rt->chip->comm; 89 if (comm_rt) { 90 comm_rt->write8(comm_rt, 0x22, 0x00, rt->opt_coax_switch); 91 comm_rt->write8(comm_rt, 0x21, 0x00, rt->opt_coax_switch); 92 } 93 } 94 95 static int usb6fire_control_set_rate(struct control_runtime *rt, int rate) 96 { 97 int ret; 98 struct usb_device *device = rt->chip->dev; 99 struct comm_runtime *comm_rt = rt->chip->comm; 100 101 if (rate < 0 || rate >= CONTROL_N_RATES) 102 return -EINVAL; 103 104 ret = usb_set_interface(device, 1, rates_altsetting[rate]); 105 if (ret < 0) 106 return ret; 107 108 /* set soundcard clock */ 109 ret = comm_rt->write16(comm_rt, 0x02, 0x01, rates_6fire_vl[rate], 110 rates_6fire_vh[rate]); 111 if (ret < 0) 112 return ret; 113 114 return 0; 115 } 116 117 static int usb6fire_control_set_channels( 118 struct control_runtime *rt, int n_analog_out, 119 int n_analog_in, bool spdif_out, bool spdif_in) 120 { 121 int ret; 122 struct comm_runtime *comm_rt = rt->chip->comm; 123 124 /* enable analog inputs and outputs 125 * (one bit per stereo-channel) */ 126 ret = comm_rt->write16(comm_rt, 0x02, 0x02, 127 (1 << (n_analog_out / 2)) - 1, 128 (1 << (n_analog_in / 2)) - 1); 129 if (ret < 0) 130 return ret; 131 132 /* disable digital inputs and outputs */ 133 /* TODO: use spdif_x to enable/disable digital channels */ 134 ret = comm_rt->write16(comm_rt, 0x02, 0x03, 0x00, 0x00); 135 if (ret < 0) 136 return ret; 137 138 return 0; 139 } 140 141 static int usb6fire_control_streaming_update(struct control_runtime *rt) 142 { 143 struct comm_runtime *comm_rt = rt->chip->comm; 144 145 if (comm_rt) { 146 if (!rt->usb_streaming && rt->digital_thru_switch) 147 usb6fire_control_set_rate(rt, 148 DIGITAL_THRU_ONLY_SAMPLERATE); 149 return comm_rt->write16(comm_rt, 0x02, 0x00, 0x00, 150 (rt->usb_streaming ? 0x01 : 0x00) | 151 (rt->digital_thru_switch ? 0x08 : 0x00)); 152 } 153 return -EINVAL; 154 } 155 156 static int usb6fire_control_output_vol_info(struct snd_kcontrol *kcontrol, 157 struct snd_ctl_elem_info *uinfo) 158 { 159 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 160 uinfo->count = 2; 161 uinfo->value.integer.min = 0; 162 uinfo->value.integer.max = 180; 163 return 0; 164 } 165 166 static int usb6fire_control_output_vol_put(struct snd_kcontrol *kcontrol, 167 struct snd_ctl_elem_value *ucontrol) 168 { 169 struct control_runtime *rt = snd_kcontrol_chip(kcontrol); 170 unsigned int ch = kcontrol->private_value; 171 int changed = 0; 172 173 if (ch > 4) { 174 snd_printk(KERN_ERR PREFIX "Invalid channel in volume control."); 175 return -EINVAL; 176 } 177 178 if (rt->output_vol[ch] != ucontrol->value.integer.value[0]) { 179 rt->output_vol[ch] = ucontrol->value.integer.value[0]; 180 rt->ovol_updated &= ~(1 << ch); 181 changed = 1; 182 } 183 if (rt->output_vol[ch + 1] != ucontrol->value.integer.value[1]) { 184 rt->output_vol[ch + 1] = ucontrol->value.integer.value[1]; 185 rt->ovol_updated &= ~(2 << ch); 186 changed = 1; 187 } 188 189 if (changed) 190 usb6fire_control_output_vol_update(rt); 191 192 return changed; 193 } 194 195 static int usb6fire_control_output_vol_get(struct snd_kcontrol *kcontrol, 196 struct snd_ctl_elem_value *ucontrol) 197 { 198 struct control_runtime *rt = snd_kcontrol_chip(kcontrol); 199 unsigned int ch = kcontrol->private_value; 200 201 if (ch > 4) { 202 snd_printk(KERN_ERR PREFIX "Invalid channel in volume control."); 203 return -EINVAL; 204 } 205 206 ucontrol->value.integer.value[0] = rt->output_vol[ch]; 207 ucontrol->value.integer.value[1] = rt->output_vol[ch + 1]; 208 return 0; 209 } 210 211 static int usb6fire_control_line_phono_info(struct snd_kcontrol *kcontrol, 212 struct snd_ctl_elem_info *uinfo) 213 { 214 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 215 uinfo->count = 1; 216 uinfo->value.enumerated.items = 2; 217 if (uinfo->value.enumerated.item > 1) 218 uinfo->value.enumerated.item = 1; 219 strcpy(uinfo->value.enumerated.name, 220 line_phono_texts[uinfo->value.enumerated.item]); 221 return 0; 222 } 223 224 static int usb6fire_control_line_phono_put(struct snd_kcontrol *kcontrol, 225 struct snd_ctl_elem_value *ucontrol) 226 { 227 struct control_runtime *rt = snd_kcontrol_chip(kcontrol); 228 int changed = 0; 229 if (rt->line_phono_switch != ucontrol->value.integer.value[0]) { 230 rt->line_phono_switch = ucontrol->value.integer.value[0]; 231 usb6fire_control_line_phono_update(rt); 232 changed = 1; 233 } 234 return changed; 235 } 236 237 static int usb6fire_control_line_phono_get(struct snd_kcontrol *kcontrol, 238 struct snd_ctl_elem_value *ucontrol) 239 { 240 struct control_runtime *rt = snd_kcontrol_chip(kcontrol); 241 ucontrol->value.integer.value[0] = rt->line_phono_switch; 242 return 0; 243 } 244 245 static int usb6fire_control_opt_coax_info(struct snd_kcontrol *kcontrol, 246 struct snd_ctl_elem_info *uinfo) 247 { 248 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 249 uinfo->count = 1; 250 uinfo->value.enumerated.items = 2; 251 if (uinfo->value.enumerated.item > 1) 252 uinfo->value.enumerated.item = 1; 253 strcpy(uinfo->value.enumerated.name, 254 opt_coax_texts[uinfo->value.enumerated.item]); 255 return 0; 256 } 257 258 static int usb6fire_control_opt_coax_put(struct snd_kcontrol *kcontrol, 259 struct snd_ctl_elem_value *ucontrol) 260 { 261 struct control_runtime *rt = snd_kcontrol_chip(kcontrol); 262 int changed = 0; 263 264 if (rt->opt_coax_switch != ucontrol->value.enumerated.item[0]) { 265 rt->opt_coax_switch = ucontrol->value.enumerated.item[0]; 266 usb6fire_control_opt_coax_update(rt); 267 changed = 1; 268 } 269 return changed; 270 } 271 272 static int usb6fire_control_opt_coax_get(struct snd_kcontrol *kcontrol, 273 struct snd_ctl_elem_value *ucontrol) 274 { 275 struct control_runtime *rt = snd_kcontrol_chip(kcontrol); 276 ucontrol->value.enumerated.item[0] = rt->opt_coax_switch; 277 return 0; 278 } 279 280 static int usb6fire_control_digital_thru_put(struct snd_kcontrol *kcontrol, 281 struct snd_ctl_elem_value *ucontrol) 282 { 283 struct control_runtime *rt = snd_kcontrol_chip(kcontrol); 284 int changed = 0; 285 286 if (rt->digital_thru_switch != ucontrol->value.integer.value[0]) { 287 rt->digital_thru_switch = ucontrol->value.integer.value[0]; 288 usb6fire_control_streaming_update(rt); 289 changed = 1; 290 } 291 return changed; 292 } 293 294 static int usb6fire_control_digital_thru_get(struct snd_kcontrol *kcontrol, 295 struct snd_ctl_elem_value *ucontrol) 296 { 297 struct control_runtime *rt = snd_kcontrol_chip(kcontrol); 298 ucontrol->value.integer.value[0] = rt->digital_thru_switch; 299 return 0; 300 } 301 302 static struct __devinitdata snd_kcontrol_new vol_elements[] = { 303 { 304 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 305 .name = "Analog Playback Volume", 306 .index = 0, 307 .private_value = 0, 308 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 309 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 310 .info = usb6fire_control_output_vol_info, 311 .get = usb6fire_control_output_vol_get, 312 .put = usb6fire_control_output_vol_put, 313 .tlv = { .p = tlv_output } 314 }, 315 { 316 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 317 .name = "Analog Playback Volume", 318 .index = 1, 319 .private_value = 2, 320 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 321 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 322 .info = usb6fire_control_output_vol_info, 323 .get = usb6fire_control_output_vol_get, 324 .put = usb6fire_control_output_vol_put, 325 .tlv = { .p = tlv_output } 326 }, 327 { 328 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 329 .name = "Analog Playback Volume", 330 .index = 2, 331 .private_value = 4, 332 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 333 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 334 .info = usb6fire_control_output_vol_info, 335 .get = usb6fire_control_output_vol_get, 336 .put = usb6fire_control_output_vol_put, 337 .tlv = { .p = tlv_output } 338 }, 339 {} 340 }; 341 342 static struct __devinitdata snd_kcontrol_new elements[] = { 343 { 344 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 345 .name = "Line/Phono Capture Route", 346 .index = 0, 347 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 348 .info = usb6fire_control_line_phono_info, 349 .get = usb6fire_control_line_phono_get, 350 .put = usb6fire_control_line_phono_put 351 }, 352 { 353 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 354 .name = "Opt/Coax Capture Route", 355 .index = 0, 356 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 357 .info = usb6fire_control_opt_coax_info, 358 .get = usb6fire_control_opt_coax_get, 359 .put = usb6fire_control_opt_coax_put 360 }, 361 { 362 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 363 .name = "Digital Thru Playback Route", 364 .index = 0, 365 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 366 .info = snd_ctl_boolean_mono_info, 367 .get = usb6fire_control_digital_thru_get, 368 .put = usb6fire_control_digital_thru_put 369 }, 370 {} 371 }; 372 373 static int usb6fire_control_add_virtual( 374 struct control_runtime *rt, 375 struct snd_card *card, 376 char *name, 377 struct snd_kcontrol_new *elems) 378 { 379 int ret; 380 int i; 381 struct snd_kcontrol *vmaster = 382 snd_ctl_make_virtual_master(name, tlv_output); 383 struct snd_kcontrol *control; 384 385 if (!vmaster) 386 return -ENOMEM; 387 ret = snd_ctl_add(card, vmaster); 388 if (ret < 0) 389 return ret; 390 391 i = 0; 392 while (elems[i].name) { 393 control = snd_ctl_new1(&elems[i], rt); 394 if (!control) 395 return -ENOMEM; 396 ret = snd_ctl_add(card, control); 397 if (ret < 0) 398 return ret; 399 ret = snd_ctl_add_slave(vmaster, control); 400 if (ret < 0) 401 return ret; 402 i++; 403 } 404 return 0; 405 } 406 407 int __devinit usb6fire_control_init(struct sfire_chip *chip) 408 { 409 int i; 410 int ret; 411 struct control_runtime *rt = kzalloc(sizeof(struct control_runtime), 412 GFP_KERNEL); 413 struct comm_runtime *comm_rt = chip->comm; 414 415 if (!rt) 416 return -ENOMEM; 417 418 rt->chip = chip; 419 rt->update_streaming = usb6fire_control_streaming_update; 420 rt->set_rate = usb6fire_control_set_rate; 421 rt->set_channels = usb6fire_control_set_channels; 422 423 i = 0; 424 while (init_data[i].type) { 425 comm_rt->write8(comm_rt, init_data[i].type, init_data[i].reg, 426 init_data[i].value); 427 i++; 428 } 429 430 usb6fire_control_opt_coax_update(rt); 431 usb6fire_control_line_phono_update(rt); 432 usb6fire_control_output_vol_update(rt); 433 usb6fire_control_streaming_update(rt); 434 435 ret = usb6fire_control_add_virtual(rt, chip->card, 436 "Master Playback Volume", vol_elements); 437 if (ret) { 438 kfree(rt); 439 snd_printk(KERN_ERR PREFIX "cannot add control.\n"); 440 return ret; 441 } 442 443 i = 0; 444 while (elements[i].name) { 445 ret = snd_ctl_add(chip->card, snd_ctl_new1(&elements[i], rt)); 446 if (ret < 0) { 447 kfree(rt); 448 snd_printk(KERN_ERR PREFIX "cannot add control.\n"); 449 return ret; 450 } 451 i++; 452 } 453 454 chip->control = rt; 455 return 0; 456 } 457 458 void usb6fire_control_abort(struct sfire_chip *chip) 459 {} 460 461 void usb6fire_control_destroy(struct sfire_chip *chip) 462 { 463 kfree(chip->control); 464 chip->control = NULL; 465 } 466