Blame view

sound/pci/asihpi/asihpi.c 80.4 KB
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  /*
   *  Asihpi soundcard
   *  Copyright (c) by AudioScience Inc <alsa@audioscience.com>
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of version 2 of the GNU General Public License as
   *   published by the Free Software Foundation;
   *
   *   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   *
   *  The following is not a condition of use, merely a request:
   *  If you modify this program, particularly if you fix errors, AudioScience Inc
   *  would appreciate it if you grant us the right to use those modifications
   *  for any purpose including commercial applications.
   */
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
24

719f82d39   Eliot Blennerhassett   ALSA: Add support...
25
  #include "hpi_internal.h"
f50efa2d9   Eliot Blennerhassett   ALSA: asihpi - Ad...
26
  #include "hpi_version.h"
719f82d39   Eliot Blennerhassett   ALSA: Add support...
27
28
  #include "hpimsginit.h"
  #include "hpioctl.h"
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
29
  #include "hpicmn.h"
719f82d39   Eliot Blennerhassett   ALSA: Add support...
30
31
32
33
34
35
36
  
  #include <linux/pci.h>
  #include <linux/init.h>
  #include <linux/jiffies.h>
  #include <linux/slab.h>
  #include <linux/time.h>
  #include <linux/wait.h>
da155d5b4   Paul Gortmaker   sound: Add module...
37
  #include <linux/module.h>
719f82d39   Eliot Blennerhassett   ALSA: Add support...
38
39
40
41
42
43
44
45
  #include <sound/core.h>
  #include <sound/control.h>
  #include <sound/pcm.h>
  #include <sound/pcm_params.h>
  #include <sound/info.h>
  #include <sound/initval.h>
  #include <sound/tlv.h>
  #include <sound/hwdep.h>
719f82d39   Eliot Blennerhassett   ALSA: Add support...
46
47
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
f50efa2d9   Eliot Blennerhassett   ALSA: asihpi - Ad...
48
49
  MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx "
  			HPI_VER_STRING);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
50

b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
51
52
53
54
55
56
57
58
59
60
61
62
  #if defined CONFIG_SND_DEBUG_VERBOSE
  /**
   * snd_printddd - very verbose debug printk
   * @format: format string
   *
   * Works like snd_printk() for debugging purposes.
   * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
   * Must set snd module debug parameter to 3 to enable at runtime.
   */
  #define snd_printddd(format, args...) \
  	__snd_printk(3, __FILE__, __LINE__, format, ##args)
  #else
b0096a656   Eliot Blennerhassett   ALSA: asihpi: Sta...
63
  #define snd_printddd(format, args...) do { } while (0)
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
64
  #endif
719f82d39   Eliot Blennerhassett   ALSA: Add support...
65
66
  static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* index 0-MAX */
  static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
a67ff6a54   Rusty Russell   ALSA: module_para...
67
68
  static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  static bool enable_hpi_hwdep = 1;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  
  module_param_array(index, int, NULL, S_IRUGO);
  MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
  
  module_param_array(id, charp, NULL, S_IRUGO);
  MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
  
  module_param_array(enable, bool, NULL, S_IRUGO);
  MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
  
  module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
  MODULE_PARM_DESC(enable_hpi_hwdep,
  		"ALSA enable HPI hwdep for AudioScience soundcard ");
  
  /* identify driver */
  #ifdef KERNEL_ALSA_BUILD
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
85
  static char *build_info = "Built using headers from kernel source";
719f82d39   Eliot Blennerhassett   ALSA: Add support...
86
87
88
  module_param(build_info, charp, S_IRUGO);
  MODULE_PARM_DESC(build_info, "built using headers from kernel source");
  #else
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
89
  static char *build_info = "Built within ALSA source";
719f82d39   Eliot Blennerhassett   ALSA: Add support...
90
91
92
93
94
95
96
97
98
  module_param(build_info, charp, S_IRUGO);
  MODULE_PARM_DESC(build_info, "built within ALSA source");
  #endif
  
  /* set to 1 to dump every control from adapter to log */
  static const int mixer_dump;
  
  #define DEFAULT_SAMPLERATE 44100
  static int adapter_fs = DEFAULT_SAMPLERATE;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
99
100
  /* defaults */
  #define PERIODS_MIN 2
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
101
  #define PERIOD_BYTES_MIN  2048
719f82d39   Eliot Blennerhassett   ALSA: Add support...
102
  #define BUFFER_BYTES_MAX (512 * 1024)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
  
  struct clk_source {
  	int source;
  	int index;
  	char *name;
  };
  
  struct clk_cache {
  	int count;
  	int has_local;
  	struct clk_source s[MAX_CLOCKSOURCES];
  };
  
  /* Per card data */
  struct snd_card_asihpi {
  	struct snd_card *card;
  	struct pci_dev *pci;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
121
  	struct hpi_adapter *hpi;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
122
123
124
  
  	u32 h_mixer;
  	struct clk_cache cc;
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
125
  	u16 can_dma;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
126
127
128
129
130
  	u16 support_grouping;
  	u16 support_mrx;
  	u16 update_interval_frames;
  	u16 in_max_chans;
  	u16 out_max_chans;
c382a5da5   Eliot Blennerhassett   ALSA: asihpi - Lo...
131
132
  	u16 in_min_chans;
  	u16 out_min_chans;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
133
134
135
136
137
138
139
  };
  
  /* Per stream data */
  struct snd_card_asihpi_pcm {
  	struct timer_list timer;
  	unsigned int respawn_timer;
  	unsigned int hpi_buffer_attached;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
140
141
  	unsigned int buffer_bytes;
  	unsigned int period_bytes;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
142
  	unsigned int bytes_per_sec;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
143
144
145
  	unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
  	unsigned int pcm_buf_dma_ofs;	/* DMA R/W offset in buffer */
  	unsigned int pcm_buf_elapsed_dma_ofs;	/* DMA R/W offset in buffer */
0b7ce9e2b   Eliot Blennerhassett   ALSA: asihpi: Han...
146
  	unsigned int drained_count;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
147
148
149
150
151
152
153
154
155
156
  	struct snd_pcm_substream *substream;
  	u32 h_stream;
  	struct hpi_format format;
  };
  
  /* universal stream verbs work with out or in stream handles */
  
  /* Functions to allow driver to give a buffer to HPI for busmastering */
  
  static u16 hpi_stream_host_buffer_attach(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  	u32 h_stream,   /* handle to outstream. */
  	u32 size_in_bytes, /* size in bytes of bus mastering buffer */
  	u32 pci_address
  )
  {
  	struct hpi_message hm;
  	struct hpi_response hr;
  	unsigned int obj = hpi_handle_object(h_stream);
  
  	if (!h_stream)
  		return HPI_ERROR_INVALID_OBJ;
  	hpi_init_message_response(&hm, &hr, obj,
  			obj == HPI_OBJ_OSTREAM ?
  				HPI_OSTREAM_HOSTBUFFER_ALLOC :
  				HPI_ISTREAM_HOSTBUFFER_ALLOC);
  
  	hpi_handle_to_indexes(h_stream, &hm.adapter_index,
  				&hm.obj_index);
  
  	hm.u.d.u.buffer.buffer_size = size_in_bytes;
  	hm.u.d.u.buffer.pci_address = pci_address;
  	hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
  	hpi_send_recv(&hm, &hr);
  	return hr.error;
  }
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
182
  static u16 hpi_stream_host_buffer_detach(u32  h_stream)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  {
  	struct hpi_message hm;
  	struct hpi_response hr;
  	unsigned int obj = hpi_handle_object(h_stream);
  
  	if (!h_stream)
  		return HPI_ERROR_INVALID_OBJ;
  
  	hpi_init_message_response(&hm, &hr,  obj,
  			obj == HPI_OBJ_OSTREAM ?
  				HPI_OSTREAM_HOSTBUFFER_FREE :
  				HPI_ISTREAM_HOSTBUFFER_FREE);
  
  	hpi_handle_to_indexes(h_stream, &hm.adapter_index,
  				&hm.obj_index);
  	hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
  	hpi_send_recv(&hm, &hr);
  	return hr.error;
  }
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
202
  static inline u16 hpi_stream_start(u32 h_stream)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
203
204
  {
  	if (hpi_handle_object(h_stream) ==  HPI_OBJ_OSTREAM)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
205
  		return hpi_outstream_start(h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
206
  	else
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
207
  		return hpi_instream_start(h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
208
  }
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
209
  static inline u16 hpi_stream_stop(u32 h_stream)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
210
211
  {
  	if (hpi_handle_object(h_stream) ==  HPI_OBJ_OSTREAM)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
212
  		return hpi_outstream_stop(h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
213
  	else
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
214
  		return hpi_instream_stop(h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
215
216
217
  }
  
  static inline u16 hpi_stream_get_info_ex(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
218
219
220
221
222
223
224
225
      u32 h_stream,
      u16        *pw_state,
      u32        *pbuffer_size,
      u32        *pdata_in_buffer,
      u32        *psample_count,
      u32        *pauxiliary_data
  )
  {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
226
  	u16 e;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
227
  	if (hpi_handle_object(h_stream)  ==  HPI_OBJ_OSTREAM)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
228
  		e = hpi_outstream_get_info_ex(h_stream, pw_state,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
229
230
231
  					pbuffer_size, pdata_in_buffer,
  					psample_count, pauxiliary_data);
  	else
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
232
  		e = hpi_instream_get_info_ex(h_stream, pw_state,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
233
234
  					pbuffer_size, pdata_in_buffer,
  					psample_count, pauxiliary_data);
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
235
  	return e;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
236
  }
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
237
  static inline u16 hpi_stream_group_add(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
238
239
240
241
  					u32 h_master,
  					u32 h_stream)
  {
  	if (hpi_handle_object(h_master) ==  HPI_OBJ_OSTREAM)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
242
  		return hpi_outstream_group_add(h_master, h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
243
  	else
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
244
  		return hpi_instream_group_add(h_master, h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
245
  }
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
246
  static inline u16 hpi_stream_group_reset(u32 h_stream)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
247
248
  {
  	if (hpi_handle_object(h_stream) ==  HPI_OBJ_OSTREAM)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
249
  		return hpi_outstream_group_reset(h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
250
  	else
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
251
  		return hpi_instream_group_reset(h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
252
  }
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
253
  static inline u16 hpi_stream_group_get_map(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
254
255
256
  				u32 h_stream, u32 *mo, u32 *mi)
  {
  	if (hpi_handle_object(h_stream) ==  HPI_OBJ_OSTREAM)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
257
  		return hpi_outstream_group_get_map(h_stream, mo, mi);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
258
  	else
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
259
  		return hpi_instream_group_get_map(h_stream, mo, mi);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  }
  
  static u16 handle_error(u16 err, int line, char *filename)
  {
  	if (err)
  		printk(KERN_WARNING
  			"in file %s, line %d: HPI error %d
  ",
  			filename, line, err);
  	return err;
  }
  
  #define hpi_handle_error(x)  handle_error(x, __LINE__, __FILE__)
  
  /***************************** GENERAL PCM ****************/
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
275
276
277
  
  static void print_hwparams(struct snd_pcm_substream *substream,
  				struct snd_pcm_hw_params *p)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
278
  {
0a17e9930   Eliot Blennerhassett   ALSA: asihpi - Re...
279
280
  	char name[16];
  	snd_pcm_debug_name(substream, name, sizeof(name));
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  	snd_printd("%s HWPARAMS
  ", name);
  	snd_printd(" samplerate %d Hz
  ", params_rate(p));
  	snd_printd(" channels %d
  ", params_channels(p));
  	snd_printd(" format %d
  ", params_format(p));
  	snd_printd(" subformat %d
  ", params_subformat(p));
  	snd_printd(" buffer %d B
  ", params_buffer_bytes(p));
  	snd_printd(" period %d B
  ", params_period_bytes(p));
  	snd_printd(" access %d
  ", params_access(p));
  	snd_printd(" period_size %d
  ", params_period_size(p));
  	snd_printd(" periods %d
  ", params_periods(p));
  	snd_printd(" buffer_size %d
  ", params_buffer_size(p));
  	snd_printd(" %d B/s
  ", params_rate(p) *
  		params_channels(p) *
  		snd_pcm_format_width(params_format(p)) / 8);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
307
  }
719f82d39   Eliot Blennerhassett   ALSA: Add support...
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  
  static snd_pcm_format_t hpi_to_alsa_formats[] = {
  	-1,			/* INVALID */
  	SNDRV_PCM_FORMAT_U8,	/* HPI_FORMAT_PCM8_UNSIGNED        1 */
  	SNDRV_PCM_FORMAT_S16,	/* HPI_FORMAT_PCM16_SIGNED         2 */
  	-1,			/* HPI_FORMAT_MPEG_L1              3 */
  	SNDRV_PCM_FORMAT_MPEG,	/* HPI_FORMAT_MPEG_L2              4 */
  	SNDRV_PCM_FORMAT_MPEG,	/* HPI_FORMAT_MPEG_L3              5 */
  	-1,			/* HPI_FORMAT_DOLBY_AC2            6 */
  	-1,			/* HPI_FORMAT_DOLBY_AC3            7 */
  	SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN      8 */
  	-1,			/* HPI_FORMAT_AA_TAGIT1_HITS       9 */
  	-1,			/* HPI_FORMAT_AA_TAGIT1_INSERTS   10 */
  	SNDRV_PCM_FORMAT_S32,	/* HPI_FORMAT_PCM32_SIGNED        11 */
  	-1,			/* HPI_FORMAT_RAW_BITSTREAM       12 */
  	-1,			/* HPI_FORMAT_AA_TAGIT1_HITS_EX1  13 */
  	SNDRV_PCM_FORMAT_FLOAT,	/* HPI_FORMAT_PCM32_FLOAT         14 */
  #if 1
  	/* ALSA can't handle 3 byte sample size together with power-of-2
  	 *  constraint on buffer_bytes, so disable this format
  	 */
  	-1
  #else
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
331
  	/* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
719f82d39   Eliot Blennerhassett   ALSA: Add support...
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  #endif
  };
  
  
  static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
  					   u16 *hpi_format)
  {
  	u16 format;
  
  	for (format = HPI_FORMAT_PCM8_UNSIGNED;
  	     format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  		if (hpi_to_alsa_formats[format] == alsa_format) {
  			*hpi_format = format;
  			return 0;
  		}
  	}
  
  	snd_printd(KERN_WARNING "failed match for alsa format %d
  ",
  		   alsa_format);
  	*hpi_format = 0;
  	return -EINVAL;
  }
  
  static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
  					 struct snd_pcm_hardware *pcmhw)
  {
  	u16 err;
  	u32 h_control;
  	u32 sample_rate;
  	int idx;
  	unsigned int rate_min = 200000;
  	unsigned int rate_max = 0;
  	unsigned int rates = 0;
  
  	if (asihpi->support_mrx) {
  		rates |= SNDRV_PCM_RATE_CONTINUOUS;
  		rates |= SNDRV_PCM_RATE_8000_96000;
  		rate_min = 8000;
  		rate_max = 100000;
  	} else {
  		/* on cards without SRC,
  		   valid rates are determined by sampleclock */
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
375
  		err = hpi_mixer_get_control(asihpi->h_mixer,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
376
377
378
379
  					  HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  					  HPI_CONTROL_SAMPLECLOCK, &h_control);
  		if (err) {
  			snd_printk(KERN_ERR
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
380
381
  				"No local sampleclock, err %d
  ", err);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
382
  		}
7bf76c33e   Eliot Blennerhassett   ALSA: asihpi - Su...
383
384
385
386
387
388
389
  		for (idx = -1; idx < 100; idx++) {
  			if (idx == -1) {
  				if (hpi_sample_clock_get_sample_rate(h_control,
  								&sample_rate))
  					continue;
  			} else if (hpi_sample_clock_query_local_rate(h_control,
  							idx, &sample_rate)) {
719f82d39   Eliot Blennerhassett   ALSA: Add support...
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
  				break;
  			}
  
  			rate_min = min(rate_min, sample_rate);
  			rate_max = max(rate_max, sample_rate);
  
  			switch (sample_rate) {
  			case 5512:
  				rates |= SNDRV_PCM_RATE_5512;
  				break;
  			case 8000:
  				rates |= SNDRV_PCM_RATE_8000;
  				break;
  			case 11025:
  				rates |= SNDRV_PCM_RATE_11025;
  				break;
  			case 16000:
  				rates |= SNDRV_PCM_RATE_16000;
  				break;
  			case 22050:
  				rates |= SNDRV_PCM_RATE_22050;
  				break;
  			case 32000:
  				rates |= SNDRV_PCM_RATE_32000;
  				break;
  			case 44100:
  				rates |= SNDRV_PCM_RATE_44100;
  				break;
  			case 48000:
  				rates |= SNDRV_PCM_RATE_48000;
  				break;
  			case 64000:
  				rates |= SNDRV_PCM_RATE_64000;
  				break;
  			case 88200:
  				rates |= SNDRV_PCM_RATE_88200;
  				break;
  			case 96000:
  				rates |= SNDRV_PCM_RATE_96000;
  				break;
  			case 176400:
  				rates |= SNDRV_PCM_RATE_176400;
  				break;
  			case 192000:
  				rates |= SNDRV_PCM_RATE_192000;
  				break;
  			default: /* some other rate */
  				rates |= SNDRV_PCM_RATE_KNOT;
  			}
  		}
  	}
719f82d39   Eliot Blennerhassett   ALSA: Add support...
441
442
443
444
445
446
447
448
449
450
451
452
453
  	pcmhw->rates = rates;
  	pcmhw->rate_min = rate_min;
  	pcmhw->rate_max = rate_max;
  }
  
  static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
  					 struct snd_pcm_hw_params *params)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  	struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  	int err;
  	u16 format;
315e8f750   Kulikov Vasiliy   ALSA: asihpi: fix...
454
  	int width;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
455
  	unsigned int bytes_per_sec;
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
456
  	print_hwparams(substream, params);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
457
458
459
460
461
462
  	err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  	if (err < 0)
  		return err;
  	err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
  	if (err)
  		return err;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
463
464
465
466
467
  	hpi_handle_error(hpi_format_create(&dpcm->format,
  			params_channels(params),
  			format, params_rate(params), 0, 0));
  
  	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
468
  		if (hpi_instream_reset(dpcm->h_stream) != 0)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
469
  			return -EINVAL;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
470
  		if (hpi_instream_set_format(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
471
472
473
474
475
  			dpcm->h_stream, &dpcm->format) != 0)
  			return -EINVAL;
  	}
  
  	dpcm->hpi_buffer_attached = 0;
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
476
  	if (card->can_dma) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
477
  		err = hpi_stream_host_buffer_attach(dpcm->h_stream,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
478
479
  			params_buffer_bytes(params),  runtime->dma_addr);
  		if (err == 0) {
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
480
  			snd_printdd(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
481
482
483
484
485
  				"stream_host_buffer_attach succeeded %u %lu
  ",
  				params_buffer_bytes(params),
  				(unsigned long)runtime->dma_addr);
  		} else {
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
486
487
  			snd_printd("stream_host_buffer_attach error %d
  ",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
488
489
490
  					err);
  			return -ENOMEM;
  		}
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
491
  		err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
492
493
  						&dpcm->hpi_buffer_attached,
  						NULL, NULL, NULL);
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
494
495
  		snd_printdd("stream_host_buffer_attach status 0x%x
  ",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
496
  				dpcm->hpi_buffer_attached);
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
497

719f82d39   Eliot Blennerhassett   ALSA: Add support...
498
499
  	}
  	bytes_per_sec = params_rate(params) * params_channels(params);
315e8f750   Kulikov Vasiliy   ALSA: asihpi: fix...
500
501
  	width = snd_pcm_format_width(params_format(params));
  	bytes_per_sec *= width;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
502
  	bytes_per_sec /= 8;
315e8f750   Kulikov Vasiliy   ALSA: asihpi: fix...
503
  	if (width < 0 || bytes_per_sec == 0)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
504
505
506
  		return -EINVAL;
  
  	dpcm->bytes_per_sec = bytes_per_sec;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
507
508
  	dpcm->buffer_bytes = params_buffer_bytes(params);
  	dpcm->period_bytes = params_period_bytes(params);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
509

719f82d39   Eliot Blennerhassett   ALSA: Add support...
510
511
  	return 0;
  }
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
512
513
514
515
516
517
  static int
  snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  	if (dpcm->hpi_buffer_attached)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
518
  		hpi_stream_host_buffer_detach(dpcm->h_stream);
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
519
520
521
522
523
524
525
526
527
528
  
  	snd_pcm_lib_free_pages(substream);
  	return 0;
  }
  
  static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
  {
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  	kfree(dpcm);
  }
719f82d39   Eliot Blennerhassett   ALSA: Add support...
529
530
531
532
533
534
  static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
  					    substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  	int expiry;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
535
536
  	expiry = HZ / 200;
  	/*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
537
  	expiry = max(expiry, 1); /* don't let it be zero! */
719f82d39   Eliot Blennerhassett   ALSA: Add support...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  	dpcm->timer.expires = jiffies + expiry;
  	dpcm->respawn_timer = 1;
  	add_timer(&dpcm->timer);
  }
  
  static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  
  	dpcm->respawn_timer = 0;
  	del_timer(&dpcm->timer);
  }
  
  static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
  					   int cmd)
  {
  	struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
  	struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  	struct snd_pcm_substream *s;
  	u16 e;
0a17e9930   Eliot Blennerhassett   ALSA: asihpi - Re...
559
  	char name[16];
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
560

0a17e9930   Eliot Blennerhassett   ALSA: asihpi - Re...
561
  	snd_pcm_debug_name(substream, name, sizeof(name));
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
562
563
  	snd_printdd("%s trigger
  ", name);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
564

719f82d39   Eliot Blennerhassett   ALSA: Add support...
565
566
567
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
  		snd_pcm_group_for_each_entry(s, substream) {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
568
569
  			struct snd_pcm_runtime *runtime = s->runtime;
  			struct snd_card_asihpi_pcm *ds = runtime->private_data;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
570
571
572
  
  			if (snd_pcm_substream_chip(s) != card)
  				continue;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
573
574
575
  			/* don't link Cap and Play */
  			if (substream->stream != s->stream)
  				continue;
0b7ce9e2b   Eliot Blennerhassett   ALSA: asihpi: Han...
576
  			ds->drained_count = 0;
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
577
  			if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
719f82d39   Eliot Blennerhassett   ALSA: Add support...
578
  				/* How do I know how much valid data is present
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
579
580
  				* in buffer? Must be at least one period!
  				* Guessing 2 periods, but if
719f82d39   Eliot Blennerhassett   ALSA: Add support...
581
582
583
  				* buffer is bigger it may contain even more
  				* data??
  				*/
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
584
  				unsigned int preload = ds->period_bytes * 1;
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
585
586
  				snd_printddd("%d preload x%x
  ", s->number, preload);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
587
  				hpi_handle_error(hpi_outstream_write_buf(
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
588
  						ds->h_stream,
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
589
  						&runtime->dma_area[0],
719f82d39   Eliot Blennerhassett   ALSA: Add support...
590
591
  						preload,
  						&ds->format));
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
592
  				ds->pcm_buf_host_rw_ofs = preload;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
593
594
595
  			}
  
  			if (card->support_grouping) {
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
596
597
  				snd_printdd("%d group
  ", s->number);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
598
  				e = hpi_stream_group_add(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
599
600
601
602
603
604
605
606
607
608
609
  					dpcm->h_stream,
  					ds->h_stream);
  				if (!e) {
  					snd_pcm_trigger_done(s, substream);
  				} else {
  					hpi_handle_error(e);
  					break;
  				}
  			} else
  				break;
  		}
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
610
611
  		snd_printdd("start
  ");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
612
613
  		/* start the master stream */
  		snd_card_asihpi_pcm_timer_start(substream);
c4ed97d9e   Eliot Blennerhassett   ALSA: asihpi - Fi...
614
  		if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
615
  			!card->can_dma)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
616
  			hpi_handle_error(hpi_stream_start(dpcm->h_stream));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
617
618
619
620
621
622
623
  		break;
  
  	case SNDRV_PCM_TRIGGER_STOP:
  		snd_card_asihpi_pcm_timer_stop(substream);
  		snd_pcm_group_for_each_entry(s, substream) {
  			if (snd_pcm_substream_chip(s) != card)
  				continue;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
624
625
626
  			/* don't link Cap and Play */
  			if (substream->stream != s->stream)
  				continue;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
627
628
629
630
631
632
  
  			/*? workaround linked streams don't
  			transition to SETUP 20070706*/
  			s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
  
  			if (card->support_grouping) {
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
633
634
  				snd_printdd("%d group
  ", s->number);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
635
636
637
638
  				snd_pcm_trigger_done(s, substream);
  			} else
  				break;
  		}
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
639
640
  		snd_printdd("stop
  ");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
641
642
  
  		/* _prepare and _hwparams reset the stream */
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
643
  		hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
644
645
  		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  			hpi_handle_error(
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
646
  				hpi_outstream_reset(dpcm->h_stream));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
647
648
  
  		if (card->support_grouping)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
649
  			hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
650
651
652
  		break;
  
  	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
653
654
  		snd_printdd("pause release
  ");
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
655
  		hpi_handle_error(hpi_stream_start(dpcm->h_stream));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
656
657
658
  		snd_card_asihpi_pcm_timer_start(substream);
  		break;
  	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
659
660
  		snd_printdd("pause
  ");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
661
  		snd_card_asihpi_pcm_timer_stop(substream);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
662
  		hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
663
664
  		break;
  	default:
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
665
666
  		snd_printd(KERN_ERR "\tINVALID
  ");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
667
668
669
670
671
  		return -EINVAL;
  	}
  
  	return 0;
  }
719f82d39   Eliot Blennerhassett   ALSA: Add support...
672
673
674
675
676
  /*algorithm outline
   Without linking degenerates to getting single stream pos etc
   Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
  */
  /*
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
677
  pcm_buf_dma_ofs=get_buf_pos(s);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
678
  for_each_linked_stream(s) {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
679
  	pcm_buf_dma_ofs=get_buf_pos(s);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
680
  	min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
681
  	new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
682
683
684
  }
  timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
  for_each_linked_stream(s) {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
685
  	s->pcm_buf_dma_ofs = min_buf_pos;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
686
  	if (new_data > period_bytes) {
719f82d39   Eliot Blennerhassett   ALSA: Add support...
687
  		if (mmap) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
688
  			irq_pos = (irq_pos + period_bytes) % buffer_bytes;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
689
  			if (playback) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
690
  				write(period_bytes);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
691
  			} else {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
692
  				read(period_bytes);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
  			}
  		}
  		snd_pcm_period_elapsed(s);
  	}
  }
  */
  
  /** Minimum of 2 modulo values.  Works correctly when the difference between
  * the values is less than half the modulus
  */
  static inline unsigned int modulo_min(unsigned int a, unsigned int b,
  					unsigned long int modulus)
  {
  	unsigned int result;
  	if (((a-b) % modulus) < (modulus/2))
  		result = b;
  	else
  		result = a;
  
  	return result;
  }
  
  /** Timer function, equivalent to interrupt service routine for cards
  */
  static void snd_card_asihpi_timer_function(unsigned long data)
  {
  	struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
720
721
  	struct snd_pcm_substream *substream = dpcm->substream;
  	struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
722
723
724
  	struct snd_pcm_runtime *runtime;
  	struct snd_pcm_substream *s;
  	unsigned int newdata = 0;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
725
  	unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
726
727
  	unsigned int remdata, xfercount, next_jiffies;
  	int first = 1;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
728
  	int loops = 0;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
729
  	u16 state;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
730
  	u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
0a17e9930   Eliot Blennerhassett   ALSA: asihpi - Re...
731
732
733
  	char name[16];
  
  	snd_pcm_debug_name(substream, name, sizeof(name));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
734

a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
735
736
  	snd_printdd("%s snd_card_asihpi_timer_function
  ", name);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
737

719f82d39   Eliot Blennerhassett   ALSA: Add support...
738
  	/* find minimum newdata and buffer pos in group */
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
739
  	snd_pcm_group_for_each_entry(s, substream) {
719f82d39   Eliot Blennerhassett   ALSA: Add support...
740
741
742
743
744
  		struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
  		runtime = s->runtime;
  
  		if (snd_pcm_substream_chip(s) != card)
  			continue;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
745
746
747
748
749
  		/* don't link Cap and Play */
  		if (substream->stream != s->stream)
  			continue;
  
  		hpi_handle_error(hpi_stream_get_info_ex(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
750
  					ds->h_stream, &state,
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
751
752
  					&buffer_size, &bytes_avail,
  					&samples_played, &on_card_bytes));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
753
754
  
  		/* number of bytes in on-card buffer */
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
755
  		runtime->delay = on_card_bytes;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
756

f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
757
758
  		if (!card->can_dma)
  			on_card_bytes = bytes_avail;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
759
  		if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
760
  			pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
761
  			if (state == HPI_STATE_STOPPED) {
0be55c453   Eliot Blennerhassett   ALSA: asihpi - Re...
762
  				if (bytes_avail == 0) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
763
  					hpi_handle_error(hpi_stream_start(ds->h_stream));
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
764
765
  					snd_printdd("P%d start
  ", s->number);
0b7ce9e2b   Eliot Blennerhassett   ALSA: asihpi: Han...
766
  					ds->drained_count = 0;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
767
768
  				}
  			} else if (state == HPI_STATE_DRAINED) {
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
769
770
  				snd_printd(KERN_WARNING "P%d drained
  ",
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
771
  						s->number);
0b7ce9e2b   Eliot Blennerhassett   ALSA: asihpi: Han...
772
  				ds->drained_count++;
0be55c453   Eliot Blennerhassett   ALSA: asihpi - Re...
773
  				if (ds->drained_count > 20) {
0b7ce9e2b   Eliot Blennerhassett   ALSA: asihpi: Han...
774
775
776
777
778
  					snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
  					continue;
  				}
  			} else {
  				ds->drained_count = 0;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
779
780
  			}
  		} else
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
781
  			pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
782
783
784
  
  		if (first) {
  			/* can't statically init min when wrap is involved */
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
785
  			min_buf_pos = pcm_buf_dma_ofs;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
786
  			newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
787
788
789
  			first = 0;
  		} else {
  			min_buf_pos =
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
790
  				modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
791
  			newdata = min(
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
792
  				(pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
793
794
  				newdata);
  		}
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
795
796
  		snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX
  ",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
797
798
799
800
  			(unsigned long)frames_to_bytes(runtime,
  						runtime->status->hw_ptr),
  			(unsigned long)frames_to_bytes(runtime,
  						runtime->control->appl_ptr));
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
801

a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
802
803
804
805
806
807
808
  		snd_printdd("%d S=%d, "
  			"rw=0x%04X, dma=0x%04X, left=0x%04X, "
  			"aux=0x%04X space=0x%04X
  ",
  			s->number, state,
  			ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
  			(int)bytes_avail,
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
809
  			(int)on_card_bytes, buffer_size-bytes_avail);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
810
  		loops++;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
811
  	}
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
812
  	pcm_buf_dma_ofs = min_buf_pos;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
813

ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
814
815
  	remdata = newdata % dpcm->period_bytes;
  	xfercount = newdata - remdata; /* a multiple of period_bytes */
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
816
817
818
819
  	/* come back when on_card_bytes has decreased enough to allow
  	   write to happen, or when data has been consumed to make another
  	   period
  	*/
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
820
821
  	if (xfercount && (on_card_bytes  > dpcm->period_bytes))
  		next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
822
  	else
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
823
  		next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
824
825
  
  	next_jiffies = max(next_jiffies, 1U);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
826
  	dpcm->timer.expires = jiffies + next_jiffies;
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
827
828
  	snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X
  ",
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
829
  			next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
830
  	snd_pcm_group_for_each_entry(s, substream) {
719f82d39   Eliot Blennerhassett   ALSA: Add support...
831
  		struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
832

ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
833
834
835
  		/* don't link Cap and Play */
  		if (substream->stream != s->stream)
  			continue;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
836
  		ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
837
838
839
840
841
842
843
844
845
846
  		if (xfercount &&
  			/* Limit use of on card fifo for playback */
  			((on_card_bytes <= ds->period_bytes) ||
  			(s->stream == SNDRV_PCM_STREAM_CAPTURE)))
  
  		{
  
  			unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
  			unsigned int xfer1, xfer2;
  			char *pd = &s->runtime->dma_area[buf_ofs];
b0096a656   Eliot Blennerhassett   ALSA: asihpi: Sta...
847
  			if (card->can_dma) { /* buffer wrap is handled at lower level */
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
  				xfer1 = xfercount;
  				xfer2 = 0;
  			} else {
  				xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
  				xfer2 = xfercount - xfer1;
  			}
  
  			if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  				snd_printddd("P%d write1 0x%04X 0x%04X
  ",
  					s->number, xfer1, buf_ofs);
  				hpi_handle_error(
  					hpi_outstream_write_buf(
  						ds->h_stream, pd, xfer1,
  						&ds->format));
  
  				if (xfer2) {
  					pd = s->runtime->dma_area;
  
  					snd_printddd("P%d write2 0x%04X 0x%04X
  ",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
869
  							s->number,
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
870
  							xfercount - xfer1, buf_ofs);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
871
872
  					hpi_handle_error(
  						hpi_outstream_write_buf(
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
873
874
  							ds->h_stream, pd,
  							xfercount - xfer1,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
875
  							&ds->format));
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
876
877
878
879
880
881
882
883
884
885
886
887
888
889
  				}
  			} else {
  				snd_printddd("C%d read1 0x%04x
  ",
  					s->number, xfer1);
  				hpi_handle_error(
  					hpi_instream_read_buf(
  						ds->h_stream,
  						pd, xfer1));
  				if (xfer2) {
  					pd = s->runtime->dma_area;
  					snd_printddd("C%d read2 0x%04x
  ",
  						s->number, xfer2);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
890
891
  					hpi_handle_error(
  						hpi_instream_read_buf(
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
892
  							ds->h_stream,
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
893
  							pd, xfer2));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
894
  				}
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
895
  			}
47a74a5d1   Eliot Blennerhassett   ALSA: asihpi - fi...
896
897
  			ds->pcm_buf_host_rw_ofs += xfercount;
  			ds->pcm_buf_elapsed_dma_ofs += xfercount;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
898
899
900
901
902
903
904
905
906
907
908
909
  			snd_pcm_period_elapsed(s);
  		}
  	}
  
  	if (dpcm->respawn_timer)
  		add_timer(&dpcm->timer);
  }
  
  /***************************** PLAYBACK OPS ****************/
  static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
  					  unsigned int cmd, void *arg)
  {
cbd757daf   Eliot Blennerhassett   ALSA: asihpi - Us...
910
911
912
913
  	char name[16];
  	snd_pcm_debug_name(substream, name, sizeof(name));
  	snd_printddd(KERN_INFO "%s ioctl %d
  ", name, cmd);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
914
915
916
917
918
919
920
921
  	return snd_pcm_lib_ioctl(substream, cmd, arg);
  }
  
  static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
  					    substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
a6477134d   Eliot Blennerhassett   ALSA: asihpi: Upd...
922
923
  	snd_printdd("P%d prepare
  ", substream->number);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
924

ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
925
  	hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
926
927
928
  	dpcm->pcm_buf_host_rw_ofs = 0;
  	dpcm->pcm_buf_dma_ofs = 0;
  	dpcm->pcm_buf_elapsed_dma_ofs = 0;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
929
930
931
932
933
934
935
936
937
  	return 0;
  }
  
  static snd_pcm_uframes_t
  snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  	snd_pcm_uframes_t ptr;
cbd757daf   Eliot Blennerhassett   ALSA: asihpi - Us...
938
939
  	char name[16];
  	snd_pcm_debug_name(substream, name, sizeof(name));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
940

ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
941
  	ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs  % dpcm->buffer_bytes);
cbd757daf   Eliot Blennerhassett   ALSA: asihpi - Us...
942
943
  	snd_printddd("%s pointer = 0x%04lx
  ", name, (unsigned long)ptr);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
944
945
  	return ptr;
  }
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
946
947
  static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
  						u32 h_stream)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
948
949
950
951
952
953
  {
  	struct hpi_format hpi_format;
  	u16 format;
  	u16 err;
  	u32 h_control;
  	u32 sample_rate = 48000;
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
954
  	u64 formats = 0;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
955
956
957
958
  
  	/* on cards without SRC, must query at valid rate,
  	* maybe set by external sync
  	*/
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
959
  	err = hpi_mixer_get_control(asihpi->h_mixer,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
960
961
962
963
  				  HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  				  HPI_CONTROL_SAMPLECLOCK, &h_control);
  
  	if (!err)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
964
  		err = hpi_sample_clock_get_sample_rate(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
965
966
967
968
  				&sample_rate);
  
  	for (format = HPI_FORMAT_PCM8_UNSIGNED;
  	     format <= HPI_FORMAT_PCM24_SIGNED; format++) {
c1d70dd9c   Eliot Blennerhassett   ALSA: asihpi - Us...
969
970
  		err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
  					format, sample_rate, 128000, 0);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
971
  		if (!err)
c1d70dd9c   Eliot Blennerhassett   ALSA: asihpi - Us...
972
  			err = hpi_outstream_query_format(h_stream, &hpi_format);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
973
  		if (!err && (hpi_to_alsa_formats[format] != -1))
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
974
  			formats |= (1ULL << hpi_to_alsa_formats[format]);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
975
  	}
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
976
  	return formats;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
977
  }
719f82d39   Eliot Blennerhassett   ALSA: Add support...
978
979
980
981
982
  static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm;
  	struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
983
  	struct snd_pcm_hardware snd_card_asihpi_playback;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
984
985
986
987
988
  	int err;
  
  	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  	if (dpcm == NULL)
  		return -ENOMEM;
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
989
  	err = hpi_outstream_open(card->hpi->adapter->index,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
990
991
992
993
994
995
996
997
998
999
1000
  			      substream->number, &dpcm->h_stream);
  	hpi_handle_error(err);
  	if (err)
  		kfree(dpcm);
  	if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
  		return -EBUSY;
  	if (err)
  		return -EIO;
  
  	/*? also check ASI5000 samplerate source
  	    If external, only support external rate.
25985edce   Lucas De Marchi   Fix common misspe...
1001
  	    If internal and other stream playing, can't switch
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1002
1003
1004
1005
1006
1007
1008
1009
  	*/
  
  	init_timer(&dpcm->timer);
  	dpcm->timer.data = (unsigned long) dpcm;
  	dpcm->timer.function = snd_card_asihpi_timer_function;
  	dpcm->substream = substream;
  	runtime->private_data = dpcm;
  	runtime->private_free = snd_card_asihpi_runtime_free;
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
1010
1011
1012
  	memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
  	snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
  	snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1013
1014
  	/*?snd_card_asihpi_playback.period_bytes_min =
  	card->out_max_chans * 4096; */
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
1015
1016
1017
1018
1019
1020
1021
1022
  	snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  	snd_card_asihpi_playback.periods_min = PERIODS_MIN;
  	snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
  	/* snd_card_asihpi_playback.fifo_size = 0; */
  	snd_card_asihpi_playback.channels_max = card->out_max_chans;
  	snd_card_asihpi_playback.channels_min = card->out_min_chans;
  	snd_card_asihpi_playback.formats =
  			snd_card_asihpi_playback_formats(card, dpcm->h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1023
1024
1025
1026
1027
1028
1029
  
  	snd_card_asihpi_pcm_samplerates(card,  &snd_card_asihpi_playback);
  
  	snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
  					SNDRV_PCM_INFO_DOUBLE |
  					SNDRV_PCM_INFO_BATCH |
  					SNDRV_PCM_INFO_BLOCK_TRANSFER |
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
1030
1031
1032
  					SNDRV_PCM_INFO_PAUSE |
  					SNDRV_PCM_INFO_MMAP |
  					SNDRV_PCM_INFO_MMAP_VALID;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1033

09c728ace   Eliot Blennerhassett   ALSA: asihpi - On...
1034
  	if (card->support_grouping) {
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1035
  		snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
09c728ace   Eliot Blennerhassett   ALSA: asihpi - On...
1036
1037
  		snd_pcm_set_sync(substream);
  	}
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1038
1039
1040
  
  	/* struct is copied, so can create initializer dynamically */
  	runtime->hw = snd_card_asihpi_playback;
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
1041
  	if (card->can_dma)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1042
1043
1044
1045
1046
1047
1048
  		err = snd_pcm_hw_constraint_pow2(runtime, 0,
  					SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  	if (err < 0)
  		return err;
  
  	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  		card->update_interval_frames);
26aebef42   Eliot Blennerhassett   ALSA: asihpi - Im...
1049

719f82d39   Eliot Blennerhassett   ALSA: Add support...
1050
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1051
  		card->update_interval_frames * 2, UINT_MAX);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1052

b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
1053
1054
  	snd_printdd("playback open
  ");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1055
1056
1057
1058
1059
1060
1061
1062
  
  	return 0;
  }
  
  static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1063
  	hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
1064
1065
  	snd_printdd("playback close
  ");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1066
1067
1068
  
  	return 0;
  }
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
  static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
  	.open = snd_card_asihpi_playback_open,
  	.close = snd_card_asihpi_playback_close,
  	.ioctl = snd_card_asihpi_playback_ioctl,
  	.hw_params = snd_card_asihpi_pcm_hw_params,
  	.hw_free = snd_card_asihpi_hw_free,
  	.prepare = snd_card_asihpi_playback_prepare,
  	.trigger = snd_card_asihpi_trigger,
  	.pointer = snd_card_asihpi_playback_pointer,
  };
  
  /***************************** CAPTURE OPS ****************/
  static snd_pcm_uframes_t
  snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
1086
1087
  	snd_printddd("capture pointer %d=%d
  ",
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1088
1089
  			substream->number, dpcm->pcm_buf_dma_ofs);
  	/* NOTE Unlike playback can't use actual samples_played
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1090
1091
1092
  		for the capture position, because those samples aren't yet in
  		the local buffer available for reading.
  	*/
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1093
  	return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
  }
  
  static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
  					 unsigned int cmd, void *arg)
  {
  	return snd_pcm_lib_ioctl(substream, cmd, arg);
  }
  
  static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1106
  	hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1107
1108
1109
  	dpcm->pcm_buf_host_rw_ofs = 0;
  	dpcm->pcm_buf_dma_ofs = 0;
  	dpcm->pcm_buf_elapsed_dma_ofs = 0;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1110

b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
1111
1112
  	snd_printdd("Capture Prepare %d
  ", substream->number);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1113
1114
  	return 0;
  }
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
1115
1116
  static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
  					u32 h_stream)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1117
1118
1119
1120
1121
1122
  {
    struct hpi_format hpi_format;
  	u16 format;
  	u16 err;
  	u32 h_control;
  	u32 sample_rate = 48000;
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
1123
  	u64 formats = 0;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1124
1125
1126
  
  	/* on cards without SRC, must query at valid rate,
  		maybe set by external sync */
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1127
  	err = hpi_mixer_get_control(asihpi->h_mixer,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1128
1129
1130
1131
  				  HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  				  HPI_CONTROL_SAMPLECLOCK, &h_control);
  
  	if (!err)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1132
  		err = hpi_sample_clock_get_sample_rate(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1133
1134
1135
1136
  			&sample_rate);
  
  	for (format = HPI_FORMAT_PCM8_UNSIGNED;
  		format <= HPI_FORMAT_PCM24_SIGNED; format++) {
c1d70dd9c   Eliot Blennerhassett   ALSA: asihpi - Us...
1137
1138
  		err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
  					format, sample_rate, 128000, 0);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1139
  		if (!err)
c1d70dd9c   Eliot Blennerhassett   ALSA: asihpi - Us...
1140
  			err = hpi_instream_query_format(h_stream, &hpi_format);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1141
  		if (!err)
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
1142
  			formats |= (1ULL << hpi_to_alsa_formats[format]);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1143
  	}
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
1144
  	return formats;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1145
  }
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1146
1147
1148
1149
1150
  static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  	struct snd_card_asihpi_pcm *dpcm;
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
1151
  	struct snd_pcm_hardware snd_card_asihpi_capture;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1152
1153
1154
1155
1156
  	int err;
  
  	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  	if (dpcm == NULL)
  		return -ENOMEM;
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
1157
1158
  	snd_printdd("capture open adapter %d stream %d
  ",
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
1159
  			card->hpi->adapter->index, substream->number);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1160
1161
  
  	err = hpi_handle_error(
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
1162
  	    hpi_instream_open(card->hpi->adapter->index,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1163
1164
1165
1166
1167
1168
1169
  			     substream->number, &dpcm->h_stream));
  	if (err)
  		kfree(dpcm);
  	if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
  		return -EBUSY;
  	if (err)
  		return -EIO;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1170
1171
1172
1173
1174
1175
  	init_timer(&dpcm->timer);
  	dpcm->timer.data = (unsigned long) dpcm;
  	dpcm->timer.function = snd_card_asihpi_timer_function;
  	dpcm->substream = substream;
  	runtime->private_data = dpcm;
  	runtime->private_free = snd_card_asihpi_runtime_free;
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
1176
1177
1178
1179
1180
1181
1182
  	memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
  	snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
  	snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
  	snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  	snd_card_asihpi_capture.periods_min = PERIODS_MIN;
  	snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
  	/* snd_card_asihpi_capture.fifo_size = 0; */
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1183
  	snd_card_asihpi_capture.channels_max = card->in_max_chans;
c382a5da5   Eliot Blennerhassett   ALSA: asihpi - Lo...
1184
  	snd_card_asihpi_capture.channels_min = card->in_min_chans;
68d533932   Eliot Blennerhassett   ALSA: asihpi - Fi...
1185
1186
  	snd_card_asihpi_capture.formats =
  		snd_card_asihpi_capture_formats(card, dpcm->h_stream);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1187
  	snd_card_asihpi_pcm_samplerates(card,  &snd_card_asihpi_capture);
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
1188
1189
1190
  	snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
  					SNDRV_PCM_INFO_MMAP |
  					SNDRV_PCM_INFO_MMAP_VALID;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1191

e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1192
1193
  	if (card->support_grouping)
  		snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1194
  	runtime->hw = snd_card_asihpi_capture;
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
1195
  	if (card->can_dma)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
  		err = snd_pcm_hw_constraint_pow2(runtime, 0,
  					SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  	if (err < 0)
  		return err;
  
  	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  		card->update_interval_frames);
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  		card->update_interval_frames * 2, UINT_MAX);
  
  	snd_pcm_set_sync(substream);
  
  	return 0;
  }
  
  static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
  {
  	struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1214
  	hpi_handle_error(hpi_instream_close(dpcm->h_stream));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1215
1216
  	return 0;
  }
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
  static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
  	.open = snd_card_asihpi_capture_open,
  	.close = snd_card_asihpi_capture_close,
  	.ioctl = snd_card_asihpi_capture_ioctl,
  	.hw_params = snd_card_asihpi_pcm_hw_params,
  	.hw_free = snd_card_asihpi_hw_free,
  	.prepare = snd_card_asihpi_capture_prepare,
  	.trigger = snd_card_asihpi_trigger,
  	.pointer = snd_card_asihpi_capture_pointer,
  };
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
1227
1228
  static int __devinit snd_card_asihpi_pcm_new(
  		struct snd_card_asihpi *asihpi, int device)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1229
1230
1231
  {
  	struct snd_pcm *pcm;
  	int err;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
1232
1233
1234
1235
1236
1237
  	u16 num_instreams, num_outstreams, x16;
  	u32 x32;
  
  	err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
  			&num_outstreams, &num_instreams,
  			&x16, &x32, &x16);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1238

e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1239
  	err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
1240
  			num_outstreams,	num_instreams, &pcm);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1241
1242
1243
  	if (err < 0)
  		return err;
  	/* pointer to ops struct is stored, dont change ops afterwards! */
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1244
1245
1246
1247
  		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  				&snd_card_asihpi_playback_mmap_ops);
  		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  				&snd_card_asihpi_capture_mmap_ops);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1248
1249
1250
  
  	pcm->private_data = asihpi;
  	pcm->info_flags = 0;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1251
  	strcpy(pcm->name, "Asihpi PCM");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
  
  	/*? do we want to emulate MMAP for non-BBM cards?
  	Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
  	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  						snd_dma_pci_data(asihpi->pci),
  						64*1024, BUFFER_BYTES_MAX);
  
  	return 0;
  }
  
  /***************************** MIXER CONTROLS ****************/
  struct hpi_control {
  	u32 h_control;
  	u16 control_type;
  	u16 src_node_type;
  	u16 src_node_index;
  	u16 dst_node_type;
  	u16 dst_node_index;
  	u16 band;
  	char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
  };
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1273
  static const char * const asihpi_tuner_band_names[] = {
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
  	"invalid",
  	"AM",
  	"FM mono",
  	"TV NTSC-M",
  	"FM stereo",
  	"AUX",
  	"TV PAL BG",
  	"TV PAL I",
  	"TV PAL DK",
  	"TV SECAM",
  };
  
  compile_time_assert(
  	(ARRAY_SIZE(asihpi_tuner_band_names) ==
  		(HPI_TUNER_BAND_LAST+1)),
  	assert_tuner_band_names_size);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1290
  static const char * const asihpi_src_names[] = {
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1291
  	"no source",
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1292
1293
1294
1295
  	"PCM",
  	"Line",
  	"Digital",
  	"Tuner",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1296
  	"RF",
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1297
1298
  	"Clock",
  	"Bitstream",
c83061357   Eliot Blennerhassett   ALSA: asihpi - Co...
1299
1300
  	"Mic",
  	"Net",
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1301
1302
  	"Analog",
  	"Adapter",
c83061357   Eliot Blennerhassett   ALSA: asihpi - Co...
1303
  	"RTP",
502f271ae   Eliot Blennerhassett   ALSA: asihpi - Up...
1304
  	"Internal"
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1305
  };
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1306
1307
1308
  
  compile_time_assert(
  	(ARRAY_SIZE(asihpi_src_names) ==
168f1b07c   Eliot Blennerhassett   ALSA: asihpi - HP...
1309
  		(HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1310
  	assert_src_names_size);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1311
  static const char * const asihpi_dst_names[] = {
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1312
  	"no destination",
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1313
1314
1315
  	"PCM",
  	"Line",
  	"Digital",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1316
  	"RF",
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1317
  	"Speaker",
c83061357   Eliot Blennerhassett   ALSA: asihpi - Co...
1318
1319
1320
  	"Net",
  	"Analog",
  	"RTP",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1321
  };
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1322
1323
1324
  
  compile_time_assert(
  	(ARRAY_SIZE(asihpi_dst_names) ==
168f1b07c   Eliot Blennerhassett   ALSA: asihpi - HP...
1325
  		(HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
  	assert_dst_names_size);
  
  static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
  				struct snd_card_asihpi *asihpi)
  {
  	int err;
  
  	err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
  	if (err < 0)
  		return err;
  	else if (mixer_dump)
  		snd_printk(KERN_INFO "added %s(%d)
  ", ctl->name, ctl->index);
  
  	return 0;
  }
  
  /* Convert HPI control name and location into ALSA control name */
  static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
  				struct hpi_control *hpi_ctl,
  				char *name)
  {
550ac6ba4   Eliot Blennerhassett   ALSA: snd-asihpi:...
1348
  	char *dir;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1349
1350
1351
1352
1353
  	memset(snd_control, 0, sizeof(*snd_control));
  	snd_control->name = hpi_ctl->name;
  	snd_control->private_value = hpi_ctl->h_control;
  	snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  	snd_control->index = 0;
550ac6ba4   Eliot Blennerhassett   ALSA: snd-asihpi:...
1354
1355
1356
  	if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
  		dir = ""; /* clock is neither capture nor playback */
  	else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1357
1358
1359
1360
  		dir = "Capture ";  /* On or towards a PCM capture destination*/
  	else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
  		(!hpi_ctl->dst_node_type))
  		dir = "Capture "; /* On a source node that is not PCM playback */
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1361
1362
  	else if (hpi_ctl->src_node_type &&
  		(hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1363
1364
1365
1366
  		(hpi_ctl->dst_node_type))
  		dir = "Monitor Playback "; /* Between an input and an output */
  	else
  		dir = "Playback "; /* PCM Playback source, or  output node */
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1367
  	if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
550ac6ba4   Eliot Blennerhassett   ALSA: snd-asihpi:...
1368
  		sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1369
1370
1371
1372
  			asihpi_src_names[hpi_ctl->src_node_type],
  			hpi_ctl->src_node_index,
  			asihpi_dst_names[hpi_ctl->dst_node_type],
  			hpi_ctl->dst_node_index,
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1373
  			dir, name);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1374
  	else if (hpi_ctl->dst_node_type) {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1375
  		sprintf(hpi_ctl->name, "%s %d %s%s",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1376
1377
  		asihpi_dst_names[hpi_ctl->dst_node_type],
  		hpi_ctl->dst_node_index,
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1378
  		dir, name);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1379
  	} else {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1380
  		sprintf(hpi_ctl->name, "%s %d %s%s",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1381
1382
  		asihpi_src_names[hpi_ctl->src_node_type],
  		hpi_ctl->src_node_index,
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1383
  		dir, name);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1384
  	}
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1385
1386
  	/* printk(KERN_INFO "Adding %s %d to %d ",  hpi_ctl->name,
  		hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
  }
  
  /*------------------------------------------------------------
     Volume controls
   ------------------------------------------------------------*/
  #define VOL_STEP_mB 1
  static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
  {
  	u32 h_control = kcontrol->private_value;
d4b06d23a   Eliot Blennerhassett   ALSA: asihpi - Vo...
1397
  	u32 count;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1398
1399
1400
1401
1402
  	u16 err;
  	/* native gains are in millibels */
  	short min_gain_mB;
  	short max_gain_mB;
  	short step_gain_mB;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1403
  	err = hpi_volume_query_range(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1404
1405
1406
1407
1408
1409
  			&min_gain_mB, &max_gain_mB, &step_gain_mB);
  	if (err) {
  		max_gain_mB = 0;
  		min_gain_mB = -10000;
  		step_gain_mB = VOL_STEP_mB;
  	}
d4b06d23a   Eliot Blennerhassett   ALSA: asihpi - Vo...
1410
1411
1412
  	err = hpi_meter_query_channels(h_control, &count);
  	if (err)
  		count = HPI_MAX_CHANNELS;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1413
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
d4b06d23a   Eliot Blennerhassett   ALSA: asihpi - Vo...
1414
  	uinfo->count = count;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
  	uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
  	uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
  	uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
  	return 0;
  }
  
  static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	short an_gain_mB[HPI_MAX_CHANNELS];
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1426
  	hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
  	ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
  	ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
  
  	return 0;
  }
  
  static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	int change;
  	u32 h_control = kcontrol->private_value;
  	short an_gain_mB[HPI_MAX_CHANNELS];
  
  	an_gain_mB[0] =
  	    (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
  	an_gain_mB[1] =
  	    (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
  	/*  change = asihpi->mixer_volume[addr][0] != left ||
  	   asihpi->mixer_volume[addr][1] != right;
  	 */
  	change = 1;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1448
  	hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1449
1450
1451
1452
  	return change;
  }
  
  static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
000477a0f   Takashi Iwai   ALSA: asihpi - Re...
1453
  #define snd_asihpi_volume_mute_info	snd_ctl_boolean_mono_info
fe0aa88ee   Eliot Blennerhassett   ALSA: asihpi - Ad...
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
  
  static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	u32 mute;
  
  	hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
  	ucontrol->value.integer.value[0] = mute ? 0 : 1;
  
  	return 0;
  }
  
  static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	int change = 1;
  	/* HPI currently only supports all or none muting of multichannel volume
  	ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
  	*/
  	int mute =  ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
  	hpi_handle_error(hpi_volume_set_mute(h_control, mute));
  	return change;
  }
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1479
1480
1481
1482
1483
  static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
  					struct hpi_control *hpi_ctl)
  {
  	struct snd_card *card = asihpi->card;
  	struct snd_kcontrol_new snd_control;
fe0aa88ee   Eliot Blennerhassett   ALSA: asihpi - Ad...
1484
1485
  	int err;
  	u32 mute;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1486

e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1487
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1488
1489
1490
1491
1492
1493
  	snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  				SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  	snd_control.info = snd_asihpi_volume_info;
  	snd_control.get = snd_asihpi_volume_get;
  	snd_control.put = snd_asihpi_volume_put;
  	snd_control.tlv.p = db_scale_100;
fe0aa88ee   Eliot Blennerhassett   ALSA: asihpi - Ad...
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
  	err = ctl_add(card, &snd_control, asihpi);
  	if (err)
  		return err;
  
  	if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
  		asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
  		snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  		snd_control.info = snd_asihpi_volume_mute_info;
  		snd_control.get = snd_asihpi_volume_mute_get;
  		snd_control.put = snd_asihpi_volume_mute_put;
  		err = ctl_add(card, &snd_control, asihpi);
  	}
  	return err;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
  }
  
  /*------------------------------------------------------------
     Level controls
   ------------------------------------------------------------*/
  static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_info *uinfo)
  {
  	u32 h_control = kcontrol->private_value;
  	u16 err;
  	short min_gain_mB;
  	short max_gain_mB;
  	short step_gain_mB;
  
  	err =
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1522
  	    hpi_level_query_range(h_control, &min_gain_mB,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
  			       &max_gain_mB, &step_gain_mB);
  	if (err) {
  		max_gain_mB = 2400;
  		min_gain_mB = -1000;
  		step_gain_mB = 100;
  	}
  
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 2;
  	uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
  	uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
  	uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
  	return 0;
  }
  
  static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
  				struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	short an_gain_mB[HPI_MAX_CHANNELS];
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1543
  	hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
  	ucontrol->value.integer.value[0] =
  	    an_gain_mB[0] / HPI_UNITS_PER_dB;
  	ucontrol->value.integer.value[1] =
  	    an_gain_mB[1] / HPI_UNITS_PER_dB;
  
  	return 0;
  }
  
  static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
  				struct snd_ctl_elem_value *ucontrol)
  {
  	int change;
  	u32 h_control = kcontrol->private_value;
  	short an_gain_mB[HPI_MAX_CHANNELS];
  
  	an_gain_mB[0] =
  	    (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
  	an_gain_mB[1] =
  	    (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
  	/*  change = asihpi->mixer_level[addr][0] != left ||
  	   asihpi->mixer_level[addr][1] != right;
  	 */
  	change = 1;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1567
  	hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
  	return change;
  }
  
  static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
  
  static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
  					struct hpi_control *hpi_ctl)
  {
  	struct snd_card *card = asihpi->card;
  	struct snd_kcontrol_new snd_control;
  
  	/* can't use 'volume' cos some nodes have volume as well */
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1580
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
  	snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  				SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  	snd_control.info = snd_asihpi_level_info;
  	snd_control.get = snd_asihpi_level_get;
  	snd_control.put = snd_asihpi_level_put;
  	snd_control.tlv.p = db_scale_level;
  
  	return ctl_add(card, &snd_control, asihpi);
  }
  
  /*------------------------------------------------------------
     AESEBU controls
   ------------------------------------------------------------*/
  
  /* AESEBU format */
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1596
1597
  static const char * const asihpi_aesebu_format_names[] = {
  	"N/A", "S/PDIF", "AES/EBU" };
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
  
  static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  	uinfo->count = 1;
  	uinfo->value.enumerated.items = 3;
  
  	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  		uinfo->value.enumerated.item =
  			uinfo->value.enumerated.items - 1;
  
  	strcpy(uinfo->value.enumerated.name,
  		asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
  
  	return 0;
  }
  
  static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
  			struct snd_ctl_elem_value *ucontrol,
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1618
  			u16 (*func)(u32, u16 *))
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1619
1620
1621
  {
  	u32 h_control = kcontrol->private_value;
  	u16 source, err;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1622
  	err = func(h_control, &source);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
  
  	/* default to N/A */
  	ucontrol->value.enumerated.item[0] = 0;
  	/* return success but set the control to N/A */
  	if (err)
  		return 0;
  	if (source == HPI_AESEBU_FORMAT_SPDIF)
  		ucontrol->value.enumerated.item[0] = 1;
  	if (source == HPI_AESEBU_FORMAT_AESEBU)
  		ucontrol->value.enumerated.item[0] = 2;
  
  	return 0;
  }
  
  static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
  			struct snd_ctl_elem_value *ucontrol,
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1639
  			 u16 (*func)(u32, u16))
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
  {
  	u32 h_control = kcontrol->private_value;
  
  	/* default to S/PDIF */
  	u16 source = HPI_AESEBU_FORMAT_SPDIF;
  
  	if (ucontrol->value.enumerated.item[0] == 1)
  		source = HPI_AESEBU_FORMAT_SPDIF;
  	if (ucontrol->value.enumerated.item[0] == 2)
  		source = HPI_AESEBU_FORMAT_AESEBU;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1650
  	if (func(h_control, source) != 0)
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1651
1652
1653
1654
1655
1656
1657
1658
  		return -EINVAL;
  
  	return 1;
  }
  
  static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol) {
  	return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1659
  					hpi_aesebu_receiver_get_format);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1660
1661
1662
1663
1664
  }
  
  static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol) {
  	return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1665
  					hpi_aesebu_receiver_set_format);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
  }
  
  static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 0X1F;
  	uinfo->value.integer.step = 1;
  
  	return 0;
  }
  
  static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol) {
  
  	u32 h_control = kcontrol->private_value;
  	u16 status;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1686
1687
  	hpi_handle_error(hpi_aesebu_receiver_get_error_status(
  					 h_control, &status));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1688
1689
1690
1691
1692
1693
1694
1695
1696
  	ucontrol->value.integer.value[0] = status;
  	return 0;
  }
  
  static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
  					struct hpi_control *hpi_ctl)
  {
  	struct snd_card *card = asihpi->card;
  	struct snd_kcontrol_new snd_control;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1697
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1698
1699
1700
1701
1702
1703
1704
1705
  	snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  	snd_control.info = snd_asihpi_aesebu_format_info;
  	snd_control.get = snd_asihpi_aesebu_rx_format_get;
  	snd_control.put = snd_asihpi_aesebu_rx_format_put;
  
  
  	if (ctl_add(card, &snd_control, asihpi) < 0)
  		return -EINVAL;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1706
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
  	snd_control.access =
  	    SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  	snd_control.info = snd_asihpi_aesebu_rxstatus_info;
  	snd_control.get = snd_asihpi_aesebu_rxstatus_get;
  
  	return ctl_add(card, &snd_control, asihpi);
  }
  
  static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol) {
  	return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1718
  					hpi_aesebu_transmitter_get_format);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1719
1720
1721
1722
1723
  }
  
  static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol) {
  	return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1724
  					hpi_aesebu_transmitter_set_format);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1725
1726
1727
1728
1729
1730
1731
1732
  }
  
  
  static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
  					struct hpi_control *hpi_ctl)
  {
  	struct snd_card *card = asihpi->card;
  	struct snd_kcontrol_new snd_control;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1733
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
  	snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  	snd_control.info = snd_asihpi_aesebu_format_info;
  	snd_control.get = snd_asihpi_aesebu_tx_format_get;
  	snd_control.put = snd_asihpi_aesebu_tx_format_put;
  
  	return ctl_add(card, &snd_control, asihpi);
  }
  
  /*------------------------------------------------------------
     Tuner controls
   ------------------------------------------------------------*/
  
  /* Gain */
  
  static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
  {
  	u32 h_control = kcontrol->private_value;
  	u16 err;
  	short idx;
  	u16 gain_range[3];
  
  	for (idx = 0; idx < 3; idx++) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1757
  		err = hpi_tuner_query_gain(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
  					  idx, &gain_range[idx]);
  		if (err != 0)
  			return err;
  	}
  
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
  	uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
  	uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
  	return 0;
  }
  
  static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	/*
  	struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  	*/
  	u32 h_control = kcontrol->private_value;
  	short gain;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1779
  	hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
  	ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
  
  	return 0;
  }
  
  static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	/*
  	struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  	*/
  	u32 h_control = kcontrol->private_value;
  	short gain;
  
  	gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1795
  	hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
  
  	return 1;
  }
  
  /* Band  */
  
  static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
  					u16 *band_list, u32 len) {
  	u32 h_control = kcontrol->private_value;
  	u16 err = 0;
  	u32 i;
  
  	for (i = 0; i < len; i++) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1809
  		err = hpi_tuner_query_band(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
  				h_control, i, &band_list[i]);
  		if (err != 0)
  			break;
  	}
  
  	if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
  		return -EIO;
  
  	return i;
  }
  
  static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
  {
  	u16 tuner_bands[HPI_TUNER_BAND_LAST];
  	int num_bands = 0;
  
  	num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  				HPI_TUNER_BAND_LAST);
  
  	if (num_bands < 0)
  		return num_bands;
  
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  	uinfo->count = 1;
  	uinfo->value.enumerated.items = num_bands;
  
  	if (num_bands > 0) {
  		if (uinfo->value.enumerated.item >=
  					uinfo->value.enumerated.items)
  			uinfo->value.enumerated.item =
  				uinfo->value.enumerated.items - 1;
  
  		strcpy(uinfo->value.enumerated.name,
  			asihpi_tuner_band_names[
  				tuner_bands[uinfo->value.enumerated.item]]);
  
  	}
  	return 0;
  }
  
  static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	/*
  	struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  	*/
  	u16 band, idx;
  	u16 tuner_bands[HPI_TUNER_BAND_LAST];
  	u32 num_bands = 0;
  
  	num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  				HPI_TUNER_BAND_LAST);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1864
  	hpi_handle_error(hpi_tuner_get_band(h_control, &band));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
  
  	ucontrol->value.enumerated.item[0] = -1;
  	for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
  		if (tuner_bands[idx] == band) {
  			ucontrol->value.enumerated.item[0] = idx;
  			break;
  		}
  
  	return 0;
  }
  
  static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	/*
  	struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  	*/
  	u32 h_control = kcontrol->private_value;
  	u16 band;
  	u16 tuner_bands[HPI_TUNER_BAND_LAST];
  	u32 num_bands = 0;
  
  	num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  			HPI_TUNER_BAND_LAST);
  
  	band = tuner_bands[ucontrol->value.enumerated.item[0]];
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1891
  	hpi_handle_error(hpi_tuner_set_band(h_control, band));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
  
  	return 1;
  }
  
  /* Freq */
  
  static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
  {
  	u32 h_control = kcontrol->private_value;
  	u16 err;
  	u16 tuner_bands[HPI_TUNER_BAND_LAST];
  	u16 num_bands = 0, band_iter, idx;
  	u32 freq_range[3], temp_freq_range[3];
  
  	num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  			HPI_TUNER_BAND_LAST);
  
  	freq_range[0] = INT_MAX;
  	freq_range[1] = 0;
  	freq_range[2] = INT_MAX;
  
  	for (band_iter = 0; band_iter < num_bands; band_iter++) {
  		for (idx = 0; idx < 3; idx++) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1916
  			err = hpi_tuner_query_frequency(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
  				idx, tuner_bands[band_iter],
  				&temp_freq_range[idx]);
  			if (err != 0)
  				return err;
  		}
  
  		/* skip band with bogus stepping */
  		if (temp_freq_range[2] <= 0)
  			continue;
  
  		if (temp_freq_range[0] < freq_range[0])
  			freq_range[0] = temp_freq_range[0];
  		if (temp_freq_range[1] > freq_range[1])
  			freq_range[1] = temp_freq_range[1];
  		if (temp_freq_range[2] < freq_range[2])
  			freq_range[2] = temp_freq_range[2];
  	}
  
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = ((int)freq_range[0]);
  	uinfo->value.integer.max = ((int)freq_range[1]);
  	uinfo->value.integer.step = ((int)freq_range[2]);
  	return 0;
  }
  
  static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	u32 freq;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1948
  	hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
  	ucontrol->value.integer.value[0] = freq;
  
  	return 0;
  }
  
  static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	u32 freq;
  
  	freq = ucontrol->value.integer.value[0];
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1961
  	hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
  
  	return 1;
  }
  
  /* Tuner control group initializer  */
  static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
  					struct hpi_control *hpi_ctl)
  {
  	struct snd_card *card = asihpi->card;
  	struct snd_kcontrol_new snd_control;
  
  	snd_control.private_value = hpi_ctl->h_control;
  	snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
1975
  	if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1976
  		asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1977
1978
1979
1980
1981
1982
1983
  		snd_control.info = snd_asihpi_tuner_gain_info;
  		snd_control.get = snd_asihpi_tuner_gain_get;
  		snd_control.put = snd_asihpi_tuner_gain_put;
  
  		if (ctl_add(card, &snd_control, asihpi) < 0)
  			return -EINVAL;
  	}
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1984
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1985
1986
1987
1988
1989
1990
  	snd_control.info = snd_asihpi_tuner_band_info;
  	snd_control.get = snd_asihpi_tuner_band_get;
  	snd_control.put = snd_asihpi_tuner_band_put;
  
  	if (ctl_add(card, &snd_control, asihpi) < 0)
  		return -EINVAL;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
1991
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
  	snd_control.info = snd_asihpi_tuner_freq_info;
  	snd_control.get = snd_asihpi_tuner_freq_get;
  	snd_control.put = snd_asihpi_tuner_freq_put;
  
  	return ctl_add(card, &snd_control, asihpi);
  }
  
  /*------------------------------------------------------------
     Meter controls
   ------------------------------------------------------------*/
  static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_info *uinfo)
  {
d4b06d23a   Eliot Blennerhassett   ALSA: asihpi - Vo...
2005
2006
2007
2008
2009
2010
  	u32 h_control = kcontrol->private_value;
  	u32 count;
  	u16 err;
  	err = hpi_meter_query_channels(h_control, &count);
  	if (err)
  		count = HPI_MAX_CHANNELS;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2011
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
d4b06d23a   Eliot Blennerhassett   ALSA: asihpi - Vo...
2012
  	uinfo->count = count;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 0x7FFFFFFF;
  	return 0;
  }
  
  /* linear values for 10dB steps */
  static int log2lin[] = {
  	0x7FFFFFFF, /* 0dB */
  	679093956,
  	214748365,
  	 67909396,
  	 21474837,
  	  6790940,
  	  2147484, /* -60dB */
  	   679094,
  	   214748, /* -80 */
  	    67909,
  	    21475, /* -100 */
  	     6791,
  	     2147,
  	      679,
  	      214,
  	       68,
  	       21,
  		7,
  		2
  };
  
  static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
  				struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	short an_gain_mB[HPI_MAX_CHANNELS], i;
  	u16 err;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2047
  	err = hpi_meter_get_peak(h_control, an_gain_mB);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
  
  	for (i = 0; i < HPI_MAX_CHANNELS; i++) {
  		if (err) {
  			ucontrol->value.integer.value[i] = 0;
  		} else if (an_gain_mB[i] >= 0) {
  			ucontrol->value.integer.value[i] =
  				an_gain_mB[i] << 16;
  		} else {
  			/* -ve is log value in millibels < -60dB,
  			* convert to (roughly!) linear,
  			*/
  			ucontrol->value.integer.value[i] =
  					log2lin[an_gain_mB[i] / -1000];
  		}
  	}
  	return 0;
  }
  
  static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
  					struct hpi_control *hpi_ctl, int subidx)
  {
  	struct snd_card *card = asihpi->card;
  	struct snd_kcontrol_new snd_control;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2071
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
  	snd_control.access =
  	    SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  	snd_control.info = snd_asihpi_meter_info;
  	snd_control.get = snd_asihpi_meter_get;
  
  	snd_control.index = subidx;
  
  	return ctl_add(card, &snd_control, asihpi);
  }
  
  /*------------------------------------------------------------
     Multiplexer controls
   ------------------------------------------------------------*/
  static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
  {
  	u32 h_control = snd_control->private_value;
  	struct hpi_control hpi_ctl;
  	int s, err;
  	for (s = 0; s < 32; s++) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2091
  		err = hpi_multiplexer_query_source(h_control, s,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
  						  &hpi_ctl.
  						  src_node_type,
  						  &hpi_ctl.
  						  src_node_index);
  		if (err)
  			break;
  	}
  	return s;
  }
  
  static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
  			       struct snd_ctl_elem_info *uinfo)
  {
  	int err;
  	u16 src_node_type, src_node_index;
  	u32 h_control = kcontrol->private_value;
  
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  	uinfo->count = 1;
  	uinfo->value.enumerated.items =
  	    snd_card_asihpi_mux_count_sources(kcontrol);
  
  	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  		uinfo->value.enumerated.item =
  		    uinfo->value.enumerated.items - 1;
  
  	err =
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2119
  	    hpi_multiplexer_query_source(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2120
2121
2122
2123
  					uinfo->value.enumerated.item,
  					&src_node_type, &src_node_index);
  
  	sprintf(uinfo->value.enumerated.name, "%s %d",
168f1b07c   Eliot Blennerhassett   ALSA: asihpi - HP...
2124
  		asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
  		src_node_index);
  	return 0;
  }
  
  static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
  			      struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	u16 source_type, source_index;
  	u16 src_node_type, src_node_index;
  	int s;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2136
  	hpi_handle_error(hpi_multiplexer_get_source(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2137
2138
2139
  				&source_type, &source_index));
  	/* Should cache this search result! */
  	for (s = 0; s < 256; s++) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2140
  		if (hpi_multiplexer_query_source(h_control, s,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
  					    &src_node_type, &src_node_index))
  			break;
  
  		if ((source_type == src_node_type)
  		    && (source_index == src_node_index)) {
  			ucontrol->value.enumerated.item[0] = s;
  			return 0;
  		}
  	}
  	snd_printd(KERN_WARNING
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2151
2152
  		"Control %x failed to match mux source %hu %hu
  ",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
  		h_control, source_type, source_index);
  	ucontrol->value.enumerated.item[0] = 0;
  	return 0;
  }
  
  static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
  			      struct snd_ctl_elem_value *ucontrol)
  {
  	int change;
  	u32 h_control = kcontrol->private_value;
  	u16 source_type, source_index;
  	u16 e;
  
  	change = 1;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2167
  	e = hpi_multiplexer_query_source(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2168
2169
2170
2171
  				    ucontrol->value.enumerated.item[0],
  				    &source_type, &source_index);
  	if (!e)
  		hpi_handle_error(
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2172
  			hpi_multiplexer_set_source(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
  						source_type, source_index));
  	return change;
  }
  
  
  static int  __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
  					struct hpi_control *hpi_ctl)
  {
  	struct snd_card *card = asihpi->card;
  	struct snd_kcontrol_new snd_control;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2183
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
  	snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  	snd_control.info = snd_asihpi_mux_info;
  	snd_control.get = snd_asihpi_mux_get;
  	snd_control.put = snd_asihpi_mux_put;
  
  	return ctl_add(card, &snd_control, asihpi);
  
  }
  
  /*------------------------------------------------------------
     Channel mode controls
   ------------------------------------------------------------*/
  static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_info *uinfo)
  {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2199
2200
2201
2202
2203
  	static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
  		"invalid",
  		"Normal", "Swap",
  		"From Left", "From Right",
  		"To Left", "To Right"
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2204
2205
2206
2207
2208
  	};
  
  	u32 h_control = kcontrol->private_value;
  	u16 mode;
  	int i;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2209
2210
  	u16 mode_map[6];
  	int valid_modes = 0;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2211
2212
2213
2214
2215
  
  	/* HPI channel mode values can be from 1 to 6
  	Some adapters only support a contiguous subset
  	*/
  	for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2216
  		if (!hpi_channel_mode_query_mode(
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2217
  			h_control, i, &mode)) {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2218
2219
2220
  			mode_map[valid_modes] = mode;
  			valid_modes++;
  			}
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2221

74eeb141d   Takashi Iwai   ALSA: asihpi - Fi...
2222
2223
  	if (!valid_modes)
  		return -EINVAL;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2224
2225
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  	uinfo->count = 1;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2226
  	uinfo->value.enumerated.items = valid_modes;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2227

e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2228
2229
  	if (uinfo->value.enumerated.item >= valid_modes)
  		uinfo->value.enumerated.item = valid_modes - 1;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2230
2231
  
  	strcpy(uinfo->value.enumerated.name,
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2232
  	       mode_names[mode_map[uinfo->value.enumerated.item]]);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2233
2234
2235
2236
2237
2238
2239
2240
2241
  
  	return 0;
  }
  
  static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
  				struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	u16 mode;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2242
  	if (hpi_channel_mode_get(h_control, &mode))
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
  		mode = 1;
  
  	ucontrol->value.enumerated.item[0] = mode - 1;
  
  	return 0;
  }
  
  static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
  				struct snd_ctl_elem_value *ucontrol)
  {
  	int change;
  	u32 h_control = kcontrol->private_value;
  
  	change = 1;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2257
  	hpi_handle_error(hpi_channel_mode_set(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
  			   ucontrol->value.enumerated.item[0] + 1));
  	return change;
  }
  
  
  static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
  					struct hpi_control *hpi_ctl)
  {
  	struct snd_card *card = asihpi->card;
  	struct snd_kcontrol_new snd_control;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2268
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
  	snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  	snd_control.info = snd_asihpi_cmode_info;
  	snd_control.get = snd_asihpi_cmode_get;
  	snd_control.put = snd_asihpi_cmode_put;
  
  	return ctl_add(card, &snd_control, asihpi);
  }
  
  /*------------------------------------------------------------
     Sampleclock source  controls
   ------------------------------------------------------------*/
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2280
2281
2282
2283
2284
2285
  static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
  	"N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
  	"SMPTE", "Digital1", "Auto", "Network", "Invalid",
  	"Prev Module",
  	"Digital2", "Digital3", "Digital4", "Digital5",
  	"Digital6", "Digital7", "Digital8"};
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
  
  static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
  {
  	struct snd_card_asihpi *asihpi =
  			(struct snd_card_asihpi *)(kcontrol->private_data);
  	struct clk_cache *clkcache = &asihpi->cc;
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  	uinfo->count = 1;
  	uinfo->value.enumerated.items = clkcache->count;
  
  	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  		uinfo->value.enumerated.item =
  				uinfo->value.enumerated.items - 1;
  
  	strcpy(uinfo->value.enumerated.name,
  	       clkcache->s[uinfo->value.enumerated.item].name);
  	return 0;
  }
  
  static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	struct snd_card_asihpi *asihpi =
  			(struct snd_card_asihpi *)(kcontrol->private_data);
  	struct clk_cache *clkcache = &asihpi->cc;
  	u32 h_control = kcontrol->private_value;
  	u16 source, srcindex = 0;
  	int i;
  
  	ucontrol->value.enumerated.item[0] = 0;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2317
  	if (hpi_sample_clock_get_source(h_control, &source))
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2318
2319
2320
  		source = 0;
  
  	if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2321
  		if (hpi_sample_clock_get_source_index(h_control, &srcindex))
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
  			srcindex = 0;
  
  	for (i = 0; i < clkcache->count; i++)
  		if ((clkcache->s[i].source == source) &&
  			(clkcache->s[i].index == srcindex))
  			break;
  
  	ucontrol->value.enumerated.item[0] = i;
  
  	return 0;
  }
  
  static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
  {
  	struct snd_card_asihpi *asihpi =
  			(struct snd_card_asihpi *)(kcontrol->private_data);
  	struct clk_cache *clkcache = &asihpi->cc;
  	int change, item;
  	u32 h_control = kcontrol->private_value;
  
  	change = 1;
  	item = ucontrol->value.enumerated.item[0];
  	if (item >= clkcache->count)
  		item = clkcache->count-1;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2347
  	hpi_handle_error(hpi_sample_clock_set_source(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2348
2349
2350
  				h_control, clkcache->s[item].source));
  
  	if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2351
  		hpi_handle_error(hpi_sample_clock_set_source_index(
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
  				h_control, clkcache->s[item].index));
  	return change;
  }
  
  /*------------------------------------------------------------
     Clkrate controls
   ------------------------------------------------------------*/
  /* Need to change this to enumerated control with list of rates */
  static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
  				   struct snd_ctl_elem_info *uinfo)
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = 8000;
  	uinfo->value.integer.max = 192000;
  	uinfo->value.integer.step = 100;
  
  	return 0;
  }
  
  static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	u32 rate;
  	u16 e;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2378
  	e = hpi_sample_clock_get_local_rate(h_control, &rate);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
  	if (!e)
  		ucontrol->value.integer.value[0] = rate;
  	else
  		ucontrol->value.integer.value[0] = 0;
  	return 0;
  }
  
  static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_value *ucontrol)
  {
  	int change;
  	u32 h_control = kcontrol->private_value;
  
  	/*  change = asihpi->mixer_clkrate[addr][0] != left ||
  	   asihpi->mixer_clkrate[addr][1] != right;
  	 */
  	change = 1;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2396
  	hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
  				      ucontrol->value.integer.value[0]));
  	return change;
  }
  
  static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
  				   struct snd_ctl_elem_info *uinfo)
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = 8000;
  	uinfo->value.integer.max = 192000;
  	uinfo->value.integer.step = 100;
  
  	return 0;
  }
  
  static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_value *ucontrol)
  {
  	u32 h_control = kcontrol->private_value;
  	u32 rate;
  	u16 e;
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2419
  	e = hpi_sample_clock_get_sample_rate(h_control, &rate);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
  	if (!e)
  		ucontrol->value.integer.value[0] = rate;
  	else
  		ucontrol->value.integer.value[0] = 0;
  	return 0;
  }
  
  static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
  					struct hpi_control *hpi_ctl)
  {
  	struct snd_card *card = asihpi->card;
  	struct snd_kcontrol_new snd_control;
  
  	struct clk_cache *clkcache = &asihpi->cc;
  	u32 hSC =  hpi_ctl->h_control;
  	int has_aes_in = 0;
  	int i, j;
  	u16 source;
  
  	snd_control.private_value = hpi_ctl->h_control;
  
  	clkcache->has_local = 0;
  
  	for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2444
  		if  (hpi_sample_clock_query_source(hSC,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
  				i, &source))
  			break;
  		clkcache->s[i].source = source;
  		clkcache->s[i].index = 0;
  		clkcache->s[i].name = sampleclock_sources[source];
  		if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  			has_aes_in = 1;
  		if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
  			clkcache->has_local = 1;
  	}
  	if (has_aes_in)
  		/* already will have picked up index 0 above */
  		for (j = 1; j < 8; j++) {
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2458
  			if (hpi_sample_clock_query_source_index(hSC,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
  				j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
  				&source))
  				break;
  			clkcache->s[i].source =
  				HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
  			clkcache->s[i].index = j;
  			clkcache->s[i].name = sampleclock_sources[
  					j+HPI_SAMPLECLOCK_SOURCE_LAST];
  			i++;
  		}
  	clkcache->count = i;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2470
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2471
2472
2473
2474
2475
2476
2477
2478
2479
  	snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
  	snd_control.info = snd_asihpi_clksrc_info;
  	snd_control.get = snd_asihpi_clksrc_get;
  	snd_control.put = snd_asihpi_clksrc_put;
  	if (ctl_add(card, &snd_control, asihpi) < 0)
  		return -EINVAL;
  
  
  	if (clkcache->has_local) {
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2480
  		asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2481
2482
2483
2484
2485
2486
2487
2488
2489
  		snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
  		snd_control.info = snd_asihpi_clklocal_info;
  		snd_control.get = snd_asihpi_clklocal_get;
  		snd_control.put = snd_asihpi_clklocal_put;
  
  
  		if (ctl_add(card, &snd_control, asihpi) < 0)
  			return -EINVAL;
  	}
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2490
  	asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
  	snd_control.access =
  	    SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  	snd_control.info = snd_asihpi_clkrate_info;
  	snd_control.get = snd_asihpi_clkrate_get;
  
  	return ctl_add(card, &snd_control, asihpi);
  }
  /*------------------------------------------------------------
     Mixer
   ------------------------------------------------------------*/
  
  static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
  {
  	struct snd_card *card = asihpi->card;
  	unsigned int idx = 0;
  	unsigned int subindex = 0;
  	int err;
  	struct hpi_control hpi_ctl, prev_ctl;
  
  	if (snd_BUG_ON(!asihpi))
  		return -EINVAL;
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2512
  	strcpy(card->mixername, "Asihpi Mixer");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2513
2514
  
  	err =
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2515
  	    hpi_mixer_open(asihpi->hpi->adapter->index,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2516
2517
2518
2519
  			  &asihpi->h_mixer);
  	hpi_handle_error(err);
  	if (err)
  		return -err;
21896bc01   Takashi Iwai   ALSA: asihpi - Fi...
2520
2521
  	memset(&prev_ctl, 0, sizeof(prev_ctl));
  	prev_ctl.control_type = -1;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2522
2523
  	for (idx = 0; idx < 2000; idx++) {
  		err = hpi_mixer_get_control_by_index(
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2524
  				asihpi->h_mixer,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
  				idx,
  				&hpi_ctl.src_node_type,
  				&hpi_ctl.src_node_index,
  				&hpi_ctl.dst_node_type,
  				&hpi_ctl.dst_node_index,
  				&hpi_ctl.control_type,
  				&hpi_ctl.h_control);
  		if (err) {
  			if (err == HPI_ERROR_CONTROL_DISABLED) {
  				if (mixer_dump)
  					snd_printk(KERN_INFO
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2536
2537
  						   "Disabled HPI Control(%d)
  ",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2538
2539
2540
2541
2542
2543
  						   idx);
  				continue;
  			} else
  				break;
  
  		}
168f1b07c   Eliot Blennerhassett   ALSA: asihpi - HP...
2544
2545
  		hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
  		hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
  
  		/* ASI50xx in SSX mode has multiple meters on the same node.
  		   Use subindex to create distinct ALSA controls
  		   for any duplicated controls.
  		*/
  		if ((hpi_ctl.control_type == prev_ctl.control_type) &&
  		    (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
  		    (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
  		    (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
  		    (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
  			subindex++;
  		else
  			subindex = 0;
  
  		prev_ctl = hpi_ctl;
  
  		switch (hpi_ctl.control_type) {
  		case HPI_CONTROL_VOLUME:
  			err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
  			break;
  		case HPI_CONTROL_LEVEL:
  			err = snd_asihpi_level_add(asihpi, &hpi_ctl);
  			break;
  		case HPI_CONTROL_MULTIPLEXER:
  			err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
  			break;
  		case HPI_CONTROL_CHANNEL_MODE:
  			err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
  			break;
  		case HPI_CONTROL_METER:
  			err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
  			break;
  		case HPI_CONTROL_SAMPLECLOCK:
  			err = snd_asihpi_sampleclock_add(
  						asihpi, &hpi_ctl);
  			break;
  		case HPI_CONTROL_CONNECTION:	/* ignore these */
  			continue;
  		case HPI_CONTROL_TUNER:
  			err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
  			break;
  		case HPI_CONTROL_AESEBU_TRANSMITTER:
  			err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
  			break;
  		case HPI_CONTROL_AESEBU_RECEIVER:
  			err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
  			break;
  		case HPI_CONTROL_VOX:
  		case HPI_CONTROL_BITSTREAM:
  		case HPI_CONTROL_MICROPHONE:
  		case HPI_CONTROL_PARAMETRIC_EQ:
  		case HPI_CONTROL_COMPANDER:
  		default:
  			if (mixer_dump)
  				snd_printk(KERN_INFO
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2601
  					"Untranslated HPI Control"
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
  					"(%d) %d %d %d %d %d
  ",
  					idx,
  					hpi_ctl.control_type,
  					hpi_ctl.src_node_type,
  					hpi_ctl.src_node_index,
  					hpi_ctl.dst_node_type,
  					hpi_ctl.dst_node_index);
  			continue;
  		};
  		if (err < 0)
  			return err;
  	}
  	if (HPI_ERROR_INVALID_OBJ_INDEX != err)
  		hpi_handle_error(err);
  
  	snd_printk(KERN_INFO "%d mixer controls found
  ", idx);
  
  	return 0;
  }
  
  /*------------------------------------------------------------
     /proc interface
   ------------------------------------------------------------*/
  
  static void
  snd_asihpi_proc_read(struct snd_info_entry *entry,
  			struct snd_info_buffer *buffer)
  {
  	struct snd_card_asihpi *asihpi = entry->private_data;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2633
2634
2635
  	u32 h_control;
  	u32 rate = 0;
  	u16 source = 0;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2636
2637
2638
2639
2640
2641
  
  	u16 num_outstreams;
  	u16 num_instreams;
  	u16 version;
  	u32 serial_number;
  	u16 type;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2642
2643
2644
2645
  	int err;
  
  	snd_iprintf(buffer, "ASIHPI driver proc file
  ");
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2646
2647
2648
2649
  
  	hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
  			&num_outstreams, &num_instreams,
  			&version, &serial_number, &type));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2650
  	snd_iprintf(buffer,
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2651
2652
2653
2654
2655
2656
2657
2658
  			"Adapter type ASI%4X
  Hardware Index %d
  "
  			"%d outstreams
  %d instreams
  ",
  			type, asihpi->hpi->adapter->index,
  			num_outstreams, num_instreams);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2659

719f82d39   Eliot Blennerhassett   ALSA: Add support...
2660
  	snd_iprintf(buffer,
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2661
2662
2663
2664
2665
  		"Serial#%d
  Hardware version %c%d
  DSP code version %03d
  ",
  		serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2666
  		((version >> 13) * 100) + ((version >> 7) & 0x3f));
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2667
  	err = hpi_mixer_get_control(asihpi->h_mixer,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2668
2669
2670
2671
  				  HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  				  HPI_CONTROL_SAMPLECLOCK, &h_control);
  
  	if (!err) {
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2672
  		err = hpi_sample_clock_get_sample_rate(h_control, &rate);
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2673
  		err += hpi_sample_clock_get_source(h_control, &source);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2674
2675
  
  		if (!err)
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2676
2677
  			snd_iprintf(buffer, "Sample Clock %dHz, source %s
  ",
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2678
2679
  			rate, sampleclock_sources[source]);
  	}
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2680
  }
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
  static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
  {
  	struct snd_info_entry *entry;
  
  	if (!snd_card_proc_new(asihpi->card, "info", &entry))
  		snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
  }
  
  /*------------------------------------------------------------
     HWDEP
   ------------------------------------------------------------*/
  
  static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
  {
  	if (enable_hpi_hwdep)
  		return 0;
  	else
  		return -ENODEV;
  
  }
  
  static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
  {
  	if (enable_hpi_hwdep)
  		return asihpi_hpi_release(file);
  	else
  		return -ENODEV;
  }
  
  static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
  				unsigned int cmd, unsigned long arg)
  {
  	if (enable_hpi_hwdep)
  		return asihpi_hpi_ioctl(file, cmd, arg);
  	else
  		return -ENODEV;
  }
  
  
  /* results in /dev/snd/hwC#D0 file for each card with index #
     also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
  */
  static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
  	int device, struct snd_hwdep **rhwdep)
  {
  	struct snd_hwdep *hw;
  	int err;
  
  	if (rhwdep)
  		*rhwdep = NULL;
  	err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
  	if (err < 0)
  		return err;
  	strcpy(hw->name, "asihpi (HPI)");
  	hw->iface = SNDRV_HWDEP_IFACE_LAST;
  	hw->ops.open = snd_asihpi_hpi_open;
  	hw->ops.ioctl = snd_asihpi_hpi_ioctl;
  	hw->ops.release = snd_asihpi_hpi_release;
  	hw->private_data = asihpi;
  	if (rhwdep)
  		*rhwdep = hw;
  	return 0;
  }
  
  /*------------------------------------------------------------
     CARD
   ------------------------------------------------------------*/
  static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
  				       const struct pci_device_id *pci_id)
  {
  	int err;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2752
  	struct hpi_adapter *hpi;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2753
2754
2755
2756
2757
  	struct snd_card *card;
  	struct snd_card_asihpi *asihpi;
  
  	u32 h_control;
  	u32 h_stream;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2758
  	u32 adapter_index;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2759
2760
2761
2762
  
  	static int dev;
  	if (dev >= SNDRV_CARDS)
  		return -ENODEV;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2763
  	/* Should this be enable[hpi->index] ? */
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2764
2765
2766
2767
  	if (!enable[dev]) {
  		dev++;
  		return -ENOENT;
  	}
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2768
  	/* Initialise low-level HPI driver */
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2769
2770
2771
  	err = asihpi_adapter_probe(pci_dev, pci_id);
  	if (err < 0)
  		return err;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2772
2773
  	hpi = pci_get_drvdata(pci_dev);
  	adapter_index = hpi->adapter->index;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2774
  	/* first try to give the card the same index as its hardware index */
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2775
2776
  	err = snd_card_create(adapter_index,
  			      id[adapter_index], THIS_MODULE,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
  			      sizeof(struct snd_card_asihpi),
  			      &card);
  	if (err < 0) {
  		/* if that fails, try the default index==next available */
  		err =
  		    snd_card_create(index[dev], id[dev],
  				    THIS_MODULE,
  				    sizeof(struct snd_card_asihpi),
  				    &card);
  		if (err < 0)
  			return err;
  		snd_printk(KERN_WARNING
e64b1a28c   Eliot Blennerhassett   ALSA: asihpi - Re...
2789
2790
  			"**** WARNING **** Adapter index %d->ALSA index %d
  ",
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2791
  			adapter_index, card->number);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2792
  	}
1225367a4   Eliot Blennerhassett   ALSA: asihpi - Ad...
2793
  	snd_card_set_dev(card, &pci_dev->dev);
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2794
  	asihpi = card->private_data;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2795
  	asihpi->card = card;
1225367a4   Eliot Blennerhassett   ALSA: asihpi - Ad...
2796
  	asihpi->pci = pci_dev;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2797
2798
2799
2800
2801
2802
2803
  	asihpi->hpi = hpi;
  
  	snd_printk(KERN_INFO "adapter ID=%4X index=%d
  ",
  			asihpi->hpi->adapter->type, adapter_index);
  
  	err = hpi_adapter_get_property(adapter_index,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2804
2805
2806
2807
  		HPI_ADAPTER_PROPERTY_CAPS1,
  		NULL, &asihpi->support_grouping);
  	if (err)
  		asihpi->support_grouping = 0;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2808
  	err = hpi_adapter_get_property(adapter_index,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2809
2810
2811
2812
  		HPI_ADAPTER_PROPERTY_CAPS2,
  		&asihpi->support_mrx, NULL);
  	if (err)
  		asihpi->support_mrx = 0;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2813
  	err = hpi_adapter_get_property(adapter_index,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2814
2815
2816
2817
  		HPI_ADAPTER_PROPERTY_INTERVAL,
  		NULL, &asihpi->update_interval_frames);
  	if (err)
  		asihpi->update_interval_frames = 512;
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
2818
  	if (!asihpi->can_dma)
26aebef42   Eliot Blennerhassett   ALSA: asihpi - Im...
2819
  		asihpi->update_interval_frames *= 2;
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2820
  	hpi_handle_error(hpi_instream_open(adapter_index,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2821
  			     0, &h_stream));
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2822
  	err = hpi_instream_host_buffer_free(h_stream);
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
2823
  	asihpi->can_dma = (!err);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2824

ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2825
  	hpi_handle_error(hpi_instream_close(h_stream));
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2826

7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2827
  	err = hpi_adapter_get_property(adapter_index,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2828
2829
2830
2831
2832
2833
  		HPI_ADAPTER_PROPERTY_CURCHANNELS,
  		&asihpi->in_max_chans, &asihpi->out_max_chans);
  	if (err) {
  		asihpi->in_max_chans = 2;
  		asihpi->out_max_chans = 2;
  	}
c382a5da5   Eliot Blennerhassett   ALSA: asihpi - Lo...
2834
2835
2836
2837
2838
2839
2840
2841
  	if (asihpi->out_max_chans > 2) { /* assume LL mode */
  		asihpi->out_min_chans = asihpi->out_max_chans;
  		asihpi->in_min_chans = asihpi->in_max_chans;
  		asihpi->support_grouping = 0;
  	} else {
  		asihpi->out_min_chans = 1;
  		asihpi->in_min_chans = 1;
  	}
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2842
2843
  	snd_printk(KERN_INFO "Has dma:%d, grouping:%d, mrx:%d
  ",
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
2844
  			asihpi->can_dma,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2845
2846
2847
  			asihpi->support_grouping,
  			asihpi->support_mrx
  	      );
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2848
  	err = snd_card_asihpi_pcm_new(asihpi, 0);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
  	if (err < 0) {
  		snd_printk(KERN_ERR "pcm_new failed
  ");
  		goto __nodev;
  	}
  	err = snd_card_asihpi_mixer_new(asihpi);
  	if (err < 0) {
  		snd_printk(KERN_ERR "mixer_new failed
  ");
  		goto __nodev;
  	}
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2860
  	err = hpi_mixer_get_control(asihpi->h_mixer,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2861
2862
2863
2864
2865
  				  HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  				  HPI_CONTROL_SAMPLECLOCK, &h_control);
  
  	if (!err)
  		err = hpi_sample_clock_set_local_rate(
ba94455c2   Eliot Blennerhassett   ALSA: asihpi - Re...
2866
  			h_control, adapter_fs);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2867
2868
2869
2870
2871
2872
  
  	snd_asihpi_proc_init(asihpi);
  
  	/* always create, can be enabled or disabled dynamically
  	    by enable_hwdep  module param*/
  	snd_asihpi_hpi_new(asihpi, 0, NULL);
f3d145aac   Eliot Blennerhassett   ALSA: asihpi: MMA...
2873
  	strcpy(card->driver, "ASIHPI");
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2874

7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2875
2876
  	sprintf(card->shortname, "AudioScience ASI%4X",
  			asihpi->hpi->adapter->type);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2877
  	sprintf(card->longname, "%s %i",
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2878
  			card->shortname, adapter_index);
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2879
  	err = snd_card_register(card);
b2e65c8e9   Eliot Blennerhassett   ALSA: asihpi - Up...
2880

719f82d39   Eliot Blennerhassett   ALSA: Add support...
2881
  	if (!err) {
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2882
  		hpi->snd_card = card;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
  		dev++;
  		return 0;
  	}
  __nodev:
  	snd_card_free(card);
  	snd_printk(KERN_ERR "snd_asihpi_probe error %d
  ", err);
  	return err;
  
  }
  
  static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
  {
7036b92d3   Eliot Blennerhassett   ALSA: asihpi - Re...
2896
2897
2898
  	struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
  	snd_card_free(hpi->snd_card);
  	hpi->snd_card = NULL;
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
  	asihpi_adapter_remove(pci_dev);
  }
  
  static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
  	{HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
  		HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
  		(kernel_ulong_t)HPI_6205},
  	{HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
  		HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
  		(kernel_ulong_t)HPI_6000},
  	{0,}
  };
  MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
  
  static struct pci_driver driver = {
3733e424c   Takashi Iwai   ALSA: Use KBUILD_...
2914
  	.name = KBUILD_MODNAME,
719f82d39   Eliot Blennerhassett   ALSA: Add support...
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
  	.id_table = asihpi_pci_tbl,
  	.probe = snd_asihpi_probe,
  	.remove = __devexit_p(snd_asihpi_remove),
  #ifdef CONFIG_PM
  /*	.suspend = snd_asihpi_suspend,
  	.resume = snd_asihpi_resume, */
  #endif
  };
  
  static int __init snd_asihpi_init(void)
  {
  	asihpi_init();
  	return pci_register_driver(&driver);
  }
  
  static void __exit snd_asihpi_exit(void)
  {
  
  	pci_unregister_driver(&driver);
  	asihpi_exit();
  }
  
  module_init(snd_asihpi_init)
  module_exit(snd_asihpi_exit)