summaryrefslogtreecommitdiff
path: root/drivers/alsa/ice1712.c
blob: 6c38364954d232cd60d1d6d7a25dd321a418748d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
    Copyright (C) 2002 Anthony Van Groningen

    Parts based on source code taken from the 
    "Env24 chipset (ICE1712) control utility" that is

    Copyright (C) 2000 by Jaroslav Kysela <perex@suse.cz>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "hardware.h"
#include "alsa_driver.h"
#include "ice1712.h"
#include "internal.h"

static int
ice1712_hw_monitor_toggle(jack_hardware_t *hw, int idx, int onoff)

{
        ice1712_t *h = (ice1712_t *) hw->private;
	snd_ctl_elem_value_t *val;
	int err;
	
	snd_ctl_elem_value_alloca (&val);
	snd_ctl_elem_value_set_interface (val, SND_CTL_ELEM_IFACE_MIXER);
	if (idx >= 8) {
		snd_ctl_elem_value_set_name (val, SPDIF_PLAYBACK_ROUTE_NAME);
		snd_ctl_elem_value_set_index (val, idx - 8);
	} else {
		snd_ctl_elem_value_set_name (val, ANALOG_PLAYBACK_ROUTE_NAME);
		snd_ctl_elem_value_set_index (val, idx);
	}
	if (onoff) {
		snd_ctl_elem_value_set_enumerated (val, 0, idx + 1);
	} else {
		snd_ctl_elem_value_set_enumerated (val, 0, 0);
	}
	if ((err = snd_ctl_elem_write (h->driver->ctl_handle, val)) != 0) {
		jack_error ("ALSA/ICE1712: (%d) cannot set input monitoring (%s)",
			    idx,snd_strerror (err));
		return -1;
	}

	return 0;
}

static int 
ice1712_set_input_monitor_mask (jack_hardware_t *hw, unsigned long mask)
     
{
	int idx;
	ice1712_t *h = (ice1712_t *) hw->private;
	
	for (idx = 0; idx < 10; idx++) {
		if (h->active_channels & (1<<idx)) {
			ice1712_hw_monitor_toggle (hw, idx, mask & (1<<idx) ? 1 : 0);
		}
	}
	hw->input_monitor_mask = mask;
	
	return 0;
}

static int 
ice1712_change_sample_clock (jack_hardware_t *hw, SampleClockMode mode) 
     
{
	return -1;
}

static void
ice1712_release (jack_hardware_t *hw)
{
	ice1712_t *h = (ice1712_t *) hw->private;
	
	if (h == 0)
	return;

	if (h->eeprom)
		free(h->eeprom);

	free(h);
}


jack_hardware_t *
jack_alsa_ice1712_hw_new (alsa_driver_t *driver)

{
	jack_hardware_t *hw;
	ice1712_t *h;
	snd_ctl_elem_value_t *val;	
	int err;

	hw = (jack_hardware_t *) malloc (sizeof (jack_hardware_t));

	hw->capabilities = Cap_HardwareMonitoring;
	hw->input_monitor_mask = 0;
	hw->private = 0;

	hw->set_input_monitor_mask = ice1712_set_input_monitor_mask;
	hw->change_sample_clock = ice1712_change_sample_clock;
	hw->release = ice1712_release;

	h = (ice1712_t *) malloc (sizeof (ice1712_t));

	h->driver = driver;

	/* Get the EEPROM (adopted from envy24control) */
	h->eeprom = (ice1712_eeprom_t *) malloc (sizeof (ice1712_eeprom_t));
	snd_ctl_elem_value_alloca (&val);
	snd_ctl_elem_value_set_interface (val, SND_CTL_ELEM_IFACE_CARD);
        snd_ctl_elem_value_set_name (val, "ICE1712 EEPROM");
        if ((err = snd_ctl_elem_read (driver->ctl_handle, val)) < 0) {
                jack_error( "ALSA/ICE1712: Unable to read EEPROM contents (%s)\n", snd_strerror (err));
                /* Recover? */
        }
        memcpy(h->eeprom, snd_ctl_elem_value_get_bytes(val), 32);

	/* determine number of pro ADC's. We're asumming that there is at least one stereo pair. 
	   Should check this first, but how?  */
	switch((h->eeprom->codec & 0xCU) >> 2) {
	case 0:
	        h->active_channels = 0x3U;
	        break;
	case 1:
	        h->active_channels = 0xfU;
	        break;
	case 2:
	        h->active_channels = 0x3fU;
	        break;
	case 3:
	        h->active_channels = 0xffU;
	        break;
	}
	/* check for SPDIF In's */
	if (h->eeprom->spdif & 0x1U) {
	        h->active_channels |= 0x300U;
	}
	
	hw->private = h;

	return hw;
}