xref: /linux/sound/pci/mixart/mixart.c (revision 9ce7677cfd7cd871adb457c80bea3b581b839641)
1 /*
2  * Driver for Digigram miXart soundcards
3  *
4  * main file with alsa callbacks
5  *
6  * Copyright (c) 2003 by Digigram <alsa@digigram.com>
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22 
23 
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/pci.h>
28 #include <linux/moduleparam.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include <sound/info.h>
32 #include <sound/control.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include "mixart.h"
36 #include "mixart_hwdep.h"
37 #include "mixart_core.h"
38 #include "mixart_mixer.h"
39 
40 #define CARD_NAME "miXart"
41 
42 MODULE_AUTHOR("Digigram <alsa@digigram.com>");
43 MODULE_DESCRIPTION("Digigram " CARD_NAME);
44 MODULE_LICENSE("GPL");
45 MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");
46 
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;             /* Index 0-MAX */
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;              /* ID for this card */
49 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;     /* Enable this card */
50 
51 module_param_array(index, int, NULL, 0444);
52 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
53 module_param_array(id, charp, NULL, 0444);
54 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
55 module_param_array(enable, bool, NULL, 0444);
56 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
57 
58 /*
59  */
60 
61 static struct pci_device_id snd_mixart_ids[] = {
62 	{ 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
63 	{ 0, }
64 };
65 
66 MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
67 
68 
69 static int mixart_set_pipe_state(mixart_mgr_t *mgr, mixart_pipe_t* pipe, int start)
70 {
71 	mixart_group_state_req_t group_state;
72 	mixart_group_state_resp_t group_state_resp;
73 	mixart_msg_t request;
74 	int err;
75 	u32 system_msg_uid;
76 
77 	switch(pipe->status) {
78 	case PIPE_RUNNING:
79 	case PIPE_CLOCK_SET:
80 		if(start) return 0; /* already started */
81 		break;
82 	case PIPE_STOPPED:
83 		if(!start) return 0; /* already stopped */
84 		break;
85 	default:
86 		snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
87 		return -EINVAL;      /* function called with wrong pipe status */
88 	}
89 
90 	system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
91 
92 	/* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
93 
94 	request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
95 	request.uid = (mixart_uid_t){0,0};
96 	request.data = &system_msg_uid;
97 	request.size = sizeof(system_msg_uid);
98 
99 	err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
100 	if(err) {
101 		snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
102 		return err;
103 	}
104 
105 	/* start or stop the pipe (1 pipe) */
106 
107 	memset(&group_state, 0, sizeof(group_state));
108 	group_state.pipe_count = 1;
109 	group_state.pipe_uid[0] = pipe->group_uid;
110 
111 	if(start)
112 		request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
113 	else
114 		request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
115 
116 	request.uid = pipe->group_uid; /*(mixart_uid_t){0,0};*/
117 	request.data = &group_state;
118 	request.size = sizeof(group_state);
119 
120 	err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
121 	if (err < 0 || group_state_resp.txx_status != 0) {
122 		snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
123 		return -EINVAL;
124 	}
125 
126 	if(start) {
127 		u32 stat;
128 
129 		group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
130 
131 		err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
132 		if (err < 0 || group_state_resp.txx_status != 0) {
133 			snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
134  			return -EINVAL;
135 		}
136 
137 		/* in case of start send a synchro top */
138 
139 		request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
140 		request.uid = (mixart_uid_t){0,0};
141 		request.data = NULL;
142 		request.size = 0;
143 
144 		err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
145 		if (err < 0 || stat != 0) {
146 			snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
147 			return -EINVAL;
148 		}
149 
150 		pipe->status = PIPE_RUNNING;
151 	}
152 	else /* !start */
153 		pipe->status = PIPE_STOPPED;
154 
155 	return 0;
156 }
157 
158 
159 static int mixart_set_clock(mixart_mgr_t *mgr, mixart_pipe_t *pipe, unsigned int rate)
160 {
161 	mixart_msg_t request;
162 	mixart_clock_properties_t clock_properties;
163 	mixart_clock_properties_resp_t clock_prop_resp;
164 	int err;
165 
166 	switch(pipe->status) {
167 	case PIPE_CLOCK_SET:
168 		break;
169 	case PIPE_RUNNING:
170 		if(rate != 0)
171 			break;
172 	default:
173 		if(rate == 0)
174 			return 0; /* nothing to do */
175 		else {
176 			snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
177 			return -EINVAL;
178 		}
179 	}
180 
181 	memset(&clock_properties, 0, sizeof(clock_properties));
182 	clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
183 	clock_properties.clock_mode = CM_STANDALONE;
184 	clock_properties.frequency = rate;
185 	clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
186 	clock_properties.uid_caller[0] = pipe->group_uid;
187 
188 	snd_printdd("mixart_set_clock to %d kHz\n", rate);
189 
190 	request.message_id = MSG_CLOCK_SET_PROPERTIES;
191 	request.uid = mgr->uid_console_manager;
192 	request.data = &clock_properties;
193 	request.size = sizeof(clock_properties);
194 
195 	err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
196 	if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
197 		snd_printk(KERN_ERR "error MSG_CLOCK_SET_PROPERTIES err=%x stat=%x mod=%x !\n", err, clock_prop_resp.status, clock_prop_resp.clock_mode);
198 		return -EINVAL;
199 	}
200 
201 	if(rate)  pipe->status = PIPE_CLOCK_SET;
202 	else      pipe->status = PIPE_RUNNING;
203 
204 	return 0;
205 }
206 
207 
208 /*
209  *  Allocate or reference output pipe for analog IOs (pcmp0/1)
210  */
211 mixart_pipe_t* snd_mixart_add_ref_pipe( mixart_t *chip, int pcm_number, int capture, int monitoring)
212 {
213 	int stream_count;
214 	mixart_pipe_t *pipe;
215 	mixart_msg_t request;
216 
217 	if(capture) {
218 		if (pcm_number == MIXART_PCM_ANALOG) {
219 			pipe = &(chip->pipe_in_ana);  /* analog inputs */
220 		} else {
221 			pipe = &(chip->pipe_in_dig); /* digital inputs */
222 		}
223 		request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
224 		stream_count = MIXART_CAPTURE_STREAMS;
225 	} else {
226 		if (pcm_number == MIXART_PCM_ANALOG) {
227 			pipe = &(chip->pipe_out_ana);  /* analog outputs */
228 		} else {
229 			pipe = &(chip->pipe_out_dig);  /* digital outputs */
230 		}
231 		request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
232 		stream_count = MIXART_PLAYBACK_STREAMS;
233 	}
234 
235 	/* a new stream is opened and there are already all streams in use */
236 	if( (monitoring == 0) && (pipe->references >= stream_count) ) {
237 		return NULL;
238 	}
239 
240 	/* pipe is not yet defined */
241 	if( pipe->status == PIPE_UNDEFINED ) {
242 		int err, i;
243 		struct {
244 			mixart_streaming_group_req_t sgroup_req;
245 			mixart_streaming_group_t sgroup_resp;
246 		} *buf;
247 
248 		snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
249 
250 		buf = kmalloc(sizeof(*buf), GFP_KERNEL);
251 		if (!buf)
252 			return NULL;
253 
254 		request.uid = (mixart_uid_t){0,0};      /* should be StreamManagerUID, but zero is OK if there is only one ! */
255 		request.data = &buf->sgroup_req;
256 		request.size = sizeof(buf->sgroup_req);
257 
258 		memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
259 
260 		buf->sgroup_req.stream_count = stream_count;
261 		buf->sgroup_req.channel_count = 2;
262 		buf->sgroup_req.latency = 256;
263 		buf->sgroup_req.connector = pipe->uid_left_connector;  /* the left connector */
264 
265 		for (i=0; i<stream_count; i++) {
266 			int j;
267 			struct mixart_flowinfo *flowinfo;
268 			struct mixart_bufferinfo *bufferinfo;
269 
270 			/* we don't yet know the format, so config 16 bit pcm audio for instance */
271 			buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
272 			buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
273 			buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
274 
275 			/* find the right bufferinfo_array */
276 			j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
277 			if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
278 
279 			buf->sgroup_req.flow_entry[i] = j;
280 
281 			flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
282 			flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(mixart_bufferinfo_t));
283 			flowinfo[j].bufferinfo_count = 1;               /* 1 will set the miXart to ring-buffer mode ! */
284 
285 			bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
286 			bufferinfo[j].buffer_address = 0;               /* buffer is not yet allocated */
287 			bufferinfo[j].available_length = 0;             /* buffer is not yet allocated */
288 
289 			/* construct the identifier of the stream buffer received in the interrupts ! */
290 			bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
291 			if(capture) {
292 				bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
293 			}
294 		}
295 
296 		err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
297 		if((err < 0) || (buf->sgroup_resp.status != 0)) {
298 			snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
299 			kfree(buf);
300 			return NULL;
301 		}
302 
303 		pipe->group_uid = buf->sgroup_resp.group;     /* id of the pipe, as returned by embedded */
304 		pipe->stream_count = buf->sgroup_resp.stream_count;
305 		/* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
306 
307 		pipe->status = PIPE_STOPPED;
308 		kfree(buf);
309 	}
310 
311 	if(monitoring)	pipe->monitoring = 1;
312 	else		pipe->references++;
313 
314 	return pipe;
315 }
316 
317 
318 int snd_mixart_kill_ref_pipe( mixart_mgr_t *mgr, mixart_pipe_t *pipe, int monitoring)
319 {
320 	int err = 0;
321 
322 	if(pipe->status == PIPE_UNDEFINED)
323 		return 0;
324 
325 	if(monitoring)
326 		pipe->monitoring = 0;
327 	else
328 		pipe->references--;
329 
330 	if((pipe->references <= 0) && (pipe->monitoring == 0)) {
331 
332 		mixart_msg_t request;
333 		mixart_delete_group_resp_t delete_resp;
334 
335 		/* release the clock */
336 		err = mixart_set_clock( mgr, pipe, 0);
337 		if( err < 0 ) {
338 			snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
339 		}
340 
341 		/* stop the pipe */
342 		err = mixart_set_pipe_state(mgr, pipe, 0);
343 		if( err < 0 ) {
344 			snd_printk(KERN_ERR "error stopping pipe!\n");
345 		}
346 
347 		request.message_id = MSG_STREAM_DELETE_GROUP;
348 		request.uid = (mixart_uid_t){0,0};
349 		request.data = &pipe->group_uid;            /* the streaming group ! */
350 		request.size = sizeof(pipe->group_uid);
351 
352 		/* delete the pipe */
353 		err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
354 		if ((err < 0) || (delete_resp.status != 0)) {
355 			snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
356 		}
357 
358 		pipe->group_uid = (mixart_uid_t){0,0};
359 		pipe->stream_count = 0;
360 		pipe->status = PIPE_UNDEFINED;
361 	}
362 
363 	return err;
364 }
365 
366 static int mixart_set_stream_state(mixart_stream_t *stream, int start)
367 {
368 	mixart_t *chip;
369 	mixart_stream_state_req_t stream_state_req;
370 	mixart_msg_t request;
371 
372 	if(!stream->substream)
373 		return -EINVAL;
374 
375 	memset(&stream_state_req, 0, sizeof(stream_state_req));
376 	stream_state_req.stream_count = 1;
377 	stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
378 	stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
379 
380 	if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
381 		request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
382 	else
383 		request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
384 
385 	request.uid = (mixart_uid_t){0,0};
386 	request.data = &stream_state_req;
387 	request.size = sizeof(stream_state_req);
388 
389 	stream->abs_period_elapsed = 0;            /* reset stream pos      */
390 	stream->buf_periods = 0;
391 	stream->buf_period_frag = 0;
392 
393 	chip = snd_pcm_substream_chip(stream->substream);
394 
395 	return snd_mixart_send_msg_nonblock(chip->mgr, &request);
396 }
397 
398 /*
399  *  Trigger callback
400  */
401 
402 static int snd_mixart_trigger(snd_pcm_substream_t *subs, int cmd)
403 {
404 	mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
405 
406 	switch (cmd) {
407 	case SNDRV_PCM_TRIGGER_START:
408 
409 		snd_printdd("SNDRV_PCM_TRIGGER_START\n");
410 
411 		/* START_STREAM */
412 		if( mixart_set_stream_state(stream, 1) )
413 			return -EINVAL;
414 
415 		stream->status = MIXART_STREAM_STATUS_RUNNING;
416 
417 		break;
418 	case SNDRV_PCM_TRIGGER_STOP:
419 
420 		/* STOP_STREAM */
421 		if( mixart_set_stream_state(stream, 0) )
422 			return -EINVAL;
423 
424 		stream->status = MIXART_STREAM_STATUS_OPEN;
425 
426 		snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
427 
428 		break;
429 
430 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
431 		/* TODO */
432 		stream->status = MIXART_STREAM_STATUS_PAUSE;
433 		snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
434 		break;
435 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
436 		/* TODO */
437 		stream->status = MIXART_STREAM_STATUS_RUNNING;
438 		snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
439 		break;
440 	default:
441 		return -EINVAL;
442 	}
443 	return 0;
444 }
445 
446 static int mixart_sync_nonblock_events(mixart_mgr_t *mgr)
447 {
448 	unsigned long timeout = jiffies + HZ;
449 	while (atomic_read(&mgr->msg_processed) > 0) {
450 		if (time_after(jiffies, timeout)) {
451 			snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
452 			return -EBUSY;
453 		}
454 		schedule_timeout_uninterruptible(1);
455 	}
456 	return 0;
457 }
458 
459 /*
460  *  prepare callback for all pcms
461  */
462 static int snd_mixart_prepare(snd_pcm_substream_t *subs)
463 {
464 	mixart_t *chip = snd_pcm_substream_chip(subs);
465 	mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
466 
467 	/* TODO de fa�on non bloquante, r�appliquer les hw_params (rate, bits, codec) */
468 
469 	snd_printdd("snd_mixart_prepare\n");
470 
471 	mixart_sync_nonblock_events(chip->mgr);
472 
473 	/* only the first stream can choose the sample rate */
474 	/* the further opened streams will be limited to its frequency (see open) */
475 	if(chip->mgr->ref_count_rate == 1)
476 		chip->mgr->sample_rate = subs->runtime->rate;
477 
478 	/* set the clock only once (first stream) on the same pipe */
479 	if(stream->pipe->references == 1) {
480 		if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
481 			return -EINVAL;
482 	}
483 
484 	return 0;
485 }
486 
487 
488 static int mixart_set_format(mixart_stream_t *stream, snd_pcm_format_t format)
489 {
490 	int err;
491 	mixart_t *chip;
492 	mixart_msg_t request;
493 	mixart_stream_param_desc_t stream_param;
494 	mixart_return_uid_t resp;
495 
496 	chip = snd_pcm_substream_chip(stream->substream);
497 
498 	memset(&stream_param, 0, sizeof(stream_param));
499 
500 	stream_param.coding_type = CT_LINEAR;
501 	stream_param.number_of_channel = stream->channels;
502 
503 	stream_param.sampling_freq = chip->mgr->sample_rate;
504 	if(stream_param.sampling_freq == 0)
505 		stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
506 
507 	switch(format){
508 	case SNDRV_PCM_FORMAT_U8:
509 		stream_param.sample_type = ST_INTEGER_8;
510 		stream_param.sample_size = 8;
511 		break;
512 	case SNDRV_PCM_FORMAT_S16_LE:
513 		stream_param.sample_type = ST_INTEGER_16LE;
514 		stream_param.sample_size = 16;
515 		break;
516 	case SNDRV_PCM_FORMAT_S16_BE:
517 		stream_param.sample_type = ST_INTEGER_16BE;
518 		stream_param.sample_size = 16;
519 		break;
520 	case SNDRV_PCM_FORMAT_S24_3LE:
521 		stream_param.sample_type = ST_INTEGER_24LE;
522 		stream_param.sample_size = 24;
523 		break;
524 	case SNDRV_PCM_FORMAT_S24_3BE:
525 		stream_param.sample_type = ST_INTEGER_24BE;
526 		stream_param.sample_size = 24;
527 		break;
528 	case SNDRV_PCM_FORMAT_FLOAT_LE:
529 		stream_param.sample_type = ST_FLOATING_POINT_32LE;
530 		stream_param.sample_size = 32;
531 		break;
532 	case  SNDRV_PCM_FORMAT_FLOAT_BE:
533 		stream_param.sample_type = ST_FLOATING_POINT_32BE;
534 		stream_param.sample_size = 32;
535 		break;
536 	default:
537 		snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
538 		return -EINVAL;
539 	}
540 
541 	snd_printdd("set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
542 		   stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
543 
544 	/* TODO: what else to configure ? */
545 	/* stream_param.samples_per_frame = 2; */
546 	/* stream_param.bytes_per_frame = 4; */
547 	/* stream_param.bytes_per_sample = 2; */
548 
549 	stream_param.pipe_count = 1;      /* set to 1 */
550 	stream_param.stream_count = 1;    /* set to 1 */
551 	stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
552 	stream_param.stream_desc[0].stream_idx = stream->substream->number;
553 
554 	request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
555 	request.uid = (mixart_uid_t){0,0};
556 	request.data = &stream_param;
557 	request.size = sizeof(stream_param);
558 
559 	err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
560 	if((err < 0) || resp.error_code) {
561 		snd_printk(KERN_ERR "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", err, resp.error_code);
562 		return -EINVAL;
563 	}
564 	return 0;
565 }
566 
567 
568 /*
569  *  HW_PARAMS callback for all pcms
570  */
571 static int snd_mixart_hw_params(snd_pcm_substream_t *subs,
572                                 snd_pcm_hw_params_t *hw)
573 {
574 	mixart_t *chip = snd_pcm_substream_chip(subs);
575 	mixart_mgr_t *mgr = chip->mgr;
576 	mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
577 	snd_pcm_format_t format;
578 	int err;
579 	int channels;
580 
581 	/* set up channels */
582 	channels = params_channels(hw);
583 
584 	/*  set up format for the stream */
585 	format = params_format(hw);
586 
587 	down(&mgr->setup_mutex);
588 
589 	/* update the stream levels */
590 	if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
591 		int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
592 		if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
593 			mixart_update_playback_stream_level(chip, is_aes, subs->number);
594 		else
595 			mixart_update_capture_stream_level( chip, is_aes);
596 	}
597 
598 	stream->channels = channels;
599 
600 	/* set the format to the board */
601 	err = mixart_set_format(stream, format);
602 	if(err < 0) {
603 		return err;
604 	}
605 
606 	/* allocate buffer */
607 	err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
608 
609 	if (err > 0) {
610 		struct mixart_bufferinfo *bufferinfo;
611 		int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number;
612 		if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) {
613 			i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */
614 		}
615 
616 		bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
617 		bufferinfo[i].buffer_address = subs->runtime->dma_addr;
618 		bufferinfo[i].available_length = subs->runtime->dma_bytes;
619 		/* bufferinfo[i].buffer_id  is already defined */
620 
621 		snd_printdd("snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", i,
622 				bufferinfo[i].buffer_address,
623 				bufferinfo[i].available_length,
624 				subs->number);
625 	}
626 	up(&mgr->setup_mutex);
627 
628 	return err;
629 }
630 
631 static int snd_mixart_hw_free(snd_pcm_substream_t *subs)
632 {
633 	mixart_t *chip = snd_pcm_substream_chip(subs);
634 	snd_pcm_lib_free_pages(subs);
635 	mixart_sync_nonblock_events(chip->mgr);
636 	return 0;
637 }
638 
639 
640 
641 /*
642  *  TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
643  */
644 static snd_pcm_hardware_t snd_mixart_analog_caps =
645 {
646 	.info             = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
647 			      SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
648 			      SNDRV_PCM_INFO_PAUSE),
649 	.formats	  = ( SNDRV_PCM_FMTBIT_U8 |
650 			      SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
651 			      SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
652 			      SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
653 	.rates            = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
654 	.rate_min         = 8000,
655 	.rate_max         = 48000,
656 	.channels_min     = 1,
657 	.channels_max     = 2,
658 	.buffer_bytes_max = (32*1024),
659 	.period_bytes_min = 256,                  /* 256 frames U8 mono*/
660 	.period_bytes_max = (16*1024),
661 	.periods_min      = 2,
662 	.periods_max      = (32*1024/256),
663 };
664 
665 static snd_pcm_hardware_t snd_mixart_digital_caps =
666 {
667 	.info             = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
668 			      SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
669 			      SNDRV_PCM_INFO_PAUSE),
670 	.formats	  = ( SNDRV_PCM_FMTBIT_U8 |
671 			      SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
672 			      SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
673 			      SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
674 	.rates            = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
675 	.rate_min         = 32000,
676 	.rate_max         = 48000,
677 	.channels_min     = 1,
678 	.channels_max     = 2,
679 	.buffer_bytes_max = (32*1024),
680 	.period_bytes_min = 256,                  /* 256 frames U8 mono*/
681 	.period_bytes_max = (16*1024),
682 	.periods_min      = 2,
683 	.periods_max      = (32*1024/256),
684 };
685 
686 
687 static int snd_mixart_playback_open(snd_pcm_substream_t *subs)
688 {
689 	mixart_t            *chip = snd_pcm_substream_chip(subs);
690 	mixart_mgr_t        *mgr = chip->mgr;
691 	snd_pcm_runtime_t   *runtime = subs->runtime;
692 	snd_pcm_t           *pcm = subs->pcm;
693 	mixart_stream_t     *stream;
694 	mixart_pipe_t       *pipe;
695 	int err = 0;
696 	int pcm_number;
697 
698 	down(&mgr->setup_mutex);
699 
700 	if ( pcm == chip->pcm ) {
701 		pcm_number = MIXART_PCM_ANALOG;
702 		runtime->hw = snd_mixart_analog_caps;
703 	} else {
704 		snd_assert ( pcm == chip->pcm_dig );
705 		pcm_number = MIXART_PCM_DIGITAL;
706 		runtime->hw = snd_mixart_digital_caps;
707 	}
708 	snd_printdd("snd_mixart_playback_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
709 
710 	/* get stream info */
711 	stream = &(chip->playback_stream[pcm_number][subs->number]);
712 
713 	if (stream->status != MIXART_STREAM_STATUS_FREE){
714 		/* streams in use */
715 		snd_printk(KERN_ERR "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
716 		err = -EBUSY;
717 		goto _exit_open;
718 	}
719 
720 	/* get pipe pointer (out pipe) */
721 	pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
722 
723 	if (pipe == NULL) {
724 		err = -EINVAL;
725 		goto _exit_open;
726 	}
727 
728 	/* start the pipe if necessary */
729 	err = mixart_set_pipe_state(chip->mgr, pipe, 1);
730 	if( err < 0 ) {
731 		snd_printk(KERN_ERR "error starting pipe!\n");
732 		snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
733 		err = -EINVAL;
734 		goto _exit_open;
735 	}
736 
737 	stream->pipe        = pipe;
738 	stream->pcm_number  = pcm_number;
739 	stream->status      = MIXART_STREAM_STATUS_OPEN;
740 	stream->substream   = subs;
741 	stream->channels    = 0; /* not configured yet */
742 
743 	runtime->private_data = stream;
744 
745 	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
746 	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
747 
748 	/* if a sample rate is already used, another stream cannot change */
749 	if(mgr->ref_count_rate++) {
750 		if(mgr->sample_rate) {
751 			runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
752 		}
753 	}
754 
755  _exit_open:
756 	up(&mgr->setup_mutex);
757 
758 	return err;
759 }
760 
761 
762 static int snd_mixart_capture_open(snd_pcm_substream_t *subs)
763 {
764 	mixart_t            *chip = snd_pcm_substream_chip(subs);
765 	mixart_mgr_t        *mgr = chip->mgr;
766 	snd_pcm_runtime_t   *runtime = subs->runtime;
767 	snd_pcm_t           *pcm = subs->pcm;
768 	mixart_stream_t     *stream;
769 	mixart_pipe_t       *pipe;
770 	int err = 0;
771 	int pcm_number;
772 
773 	down(&mgr->setup_mutex);
774 
775 	if ( pcm == chip->pcm ) {
776 		pcm_number = MIXART_PCM_ANALOG;
777 		runtime->hw = snd_mixart_analog_caps;
778 	} else {
779 		snd_assert ( pcm == chip->pcm_dig );
780 		pcm_number = MIXART_PCM_DIGITAL;
781 		runtime->hw = snd_mixart_digital_caps;
782 	}
783 
784 	runtime->hw.channels_min = 2; /* for instance, no mono */
785 
786 	snd_printdd("snd_mixart_capture_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
787 
788 	/* get stream info */
789 	stream = &(chip->capture_stream[pcm_number]);
790 
791 	if (stream->status != MIXART_STREAM_STATUS_FREE){
792 		/* streams in use */
793 		snd_printk(KERN_ERR "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
794 		err = -EBUSY;
795 		goto _exit_open;
796 	}
797 
798 	/* get pipe pointer (in pipe) */
799 	pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
800 
801 	if (pipe == NULL) {
802 		err = -EINVAL;
803 		goto _exit_open;
804 	}
805 
806 	/* start the pipe if necessary */
807 	err = mixart_set_pipe_state(chip->mgr, pipe, 1);
808 	if( err < 0 ) {
809 		snd_printk(KERN_ERR "error starting pipe!\n");
810 		snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
811 		err = -EINVAL;
812 		goto _exit_open;
813 	}
814 
815 	stream->pipe        = pipe;
816 	stream->pcm_number  = pcm_number;
817 	stream->status      = MIXART_STREAM_STATUS_OPEN;
818 	stream->substream   = subs;
819 	stream->channels    = 0; /* not configured yet */
820 
821 	runtime->private_data = stream;
822 
823 	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
824 	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
825 
826 	/* if a sample rate is already used, another stream cannot change */
827 	if(mgr->ref_count_rate++) {
828 		if(mgr->sample_rate) {
829 			runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
830 		}
831 	}
832 
833  _exit_open:
834 	up(&mgr->setup_mutex);
835 
836 	return err;
837 }
838 
839 
840 
841 static int snd_mixart_close(snd_pcm_substream_t *subs)
842 {
843 	mixart_t *chip = snd_pcm_substream_chip(subs);
844 	mixart_mgr_t *mgr = chip->mgr;
845 	mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
846 
847 	down(&mgr->setup_mutex);
848 
849 	snd_printdd("snd_mixart_close C%d/P%d/Sub%d\n", chip->chip_idx, stream->pcm_number, subs->number);
850 
851 	/* sample rate released */
852 	if(--mgr->ref_count_rate == 0) {
853 		mgr->sample_rate = 0;
854 	}
855 
856 	/* delete pipe */
857 	if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
858 
859 		snd_printk(KERN_ERR "error snd_mixart_kill_ref_pipe C%dP%d\n", chip->chip_idx, stream->pcm_number);
860 	}
861 
862 	stream->pipe      = NULL;
863 	stream->status    = MIXART_STREAM_STATUS_FREE;
864 	stream->substream = NULL;
865 
866 	up(&mgr->setup_mutex);
867 	return 0;
868 }
869 
870 
871 static snd_pcm_uframes_t snd_mixart_stream_pointer(snd_pcm_substream_t * subs)
872 {
873 	snd_pcm_runtime_t *runtime = subs->runtime;
874 	mixart_stream_t   *stream  = (mixart_stream_t*)runtime->private_data;
875 
876 	return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
877 }
878 
879 
880 
881 static snd_pcm_ops_t snd_mixart_playback_ops = {
882 	.open      = snd_mixart_playback_open,
883 	.close     = snd_mixart_close,
884 	.ioctl     = snd_pcm_lib_ioctl,
885 	.prepare   = snd_mixart_prepare,
886 	.hw_params = snd_mixart_hw_params,
887 	.hw_free   = snd_mixart_hw_free,
888 	.trigger   = snd_mixart_trigger,
889 	.pointer   = snd_mixart_stream_pointer,
890 };
891 
892 static snd_pcm_ops_t snd_mixart_capture_ops = {
893 	.open      = snd_mixart_capture_open,
894 	.close     = snd_mixart_close,
895 	.ioctl     = snd_pcm_lib_ioctl,
896 	.prepare   = snd_mixart_prepare,
897 	.hw_params = snd_mixart_hw_params,
898 	.hw_free   = snd_mixart_hw_free,
899 	.trigger   = snd_mixart_trigger,
900 	.pointer   = snd_mixart_stream_pointer,
901 };
902 
903 static void preallocate_buffers(mixart_t *chip, snd_pcm_t *pcm)
904 {
905 #if 0
906 	snd_pcm_substream_t *subs;
907 	int stream;
908 
909 	for (stream = 0; stream < 2; stream++) {
910 		int idx = 0;
911 		for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
912 			/* set up the unique device id with the chip index */
913 			subs->dma_device.id = subs->pcm->device << 16 |
914 				subs->stream << 8 | (subs->number + 1) |
915 				(chip->chip_idx + 1) << 24;
916 	}
917 #endif
918 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
919 					      snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
920 }
921 
922 /*
923  */
924 static int snd_mixart_pcm_analog(mixart_t *chip)
925 {
926 	int err;
927 	snd_pcm_t *pcm;
928 	char name[32];
929 
930 	sprintf(name, "miXart analog %d", chip->chip_idx);
931 	if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
932 			       MIXART_PLAYBACK_STREAMS,
933 			       MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
934 		snd_printk(KERN_ERR "cannot create the analog pcm %d\n", chip->chip_idx);
935 		return err;
936 	}
937 
938 	pcm->private_data = chip;
939 
940 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
941 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
942 
943 	pcm->info_flags = 0;
944 	strcpy(pcm->name, name);
945 
946 	preallocate_buffers(chip, pcm);
947 
948 	chip->pcm = pcm;
949 	return 0;
950 }
951 
952 
953 /*
954  */
955 static int snd_mixart_pcm_digital(mixart_t *chip)
956 {
957 	int err;
958 	snd_pcm_t *pcm;
959 	char name[32];
960 
961 	sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
962 	if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
963 			       MIXART_PLAYBACK_STREAMS,
964 			       MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
965 		snd_printk(KERN_ERR "cannot create the digital pcm %d\n", chip->chip_idx);
966 		return err;
967 	}
968 
969 	pcm->private_data = chip;
970 
971 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
972 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
973 
974 	pcm->info_flags = 0;
975 	strcpy(pcm->name, name);
976 
977 	preallocate_buffers(chip, pcm);
978 
979 	chip->pcm_dig = pcm;
980 	return 0;
981 }
982 
983 static int snd_mixart_chip_free(mixart_t *chip)
984 {
985 	kfree(chip);
986 	return 0;
987 }
988 
989 static int snd_mixart_chip_dev_free(snd_device_t *device)
990 {
991 	mixart_t *chip = device->device_data;
992 	return snd_mixart_chip_free(chip);
993 }
994 
995 
996 /*
997  */
998 static int __devinit snd_mixart_create(mixart_mgr_t *mgr, snd_card_t *card, int idx)
999 {
1000 	int err;
1001 	mixart_t *chip;
1002 	static snd_device_ops_t ops = {
1003 		.dev_free = snd_mixart_chip_dev_free,
1004 	};
1005 
1006 	mgr->chip[idx] = chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1007 	if (! chip) {
1008 		snd_printk(KERN_ERR "cannot allocate chip\n");
1009 		return -ENOMEM;
1010 	}
1011 
1012 	chip->card = card;
1013 	chip->chip_idx = idx;
1014 	chip->mgr = mgr;
1015 
1016 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1017 		snd_mixart_chip_free(chip);
1018 		return err;
1019 	}
1020 
1021 	snd_card_set_dev(card, &mgr->pci->dev);
1022 
1023 	return 0;
1024 }
1025 
1026 int snd_mixart_create_pcm(mixart_t* chip)
1027 {
1028 	int err;
1029 
1030 	err = snd_mixart_pcm_analog(chip);
1031 	if (err < 0)
1032 		return err;
1033 
1034 	if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
1035 
1036 		err = snd_mixart_pcm_digital(chip);
1037 		if (err < 0)
1038 			return err;
1039 	}
1040 	return err;
1041 }
1042 
1043 
1044 /*
1045  * release all the cards assigned to a manager instance
1046  */
1047 static int snd_mixart_free(mixart_mgr_t *mgr)
1048 {
1049 	unsigned int i;
1050 
1051 	for (i = 0; i < mgr->num_cards; i++) {
1052 		if (mgr->chip[i])
1053 			snd_card_free(mgr->chip[i]->card);
1054 	}
1055 
1056 	/* stop mailbox */
1057 	snd_mixart_exit_mailbox(mgr);
1058 
1059 	/* release irq  */
1060 	if (mgr->irq >= 0)
1061 		free_irq(mgr->irq, (void *)mgr);
1062 
1063 	/* reset board if some firmware was loaded */
1064 	if(mgr->dsp_loaded) {
1065 		snd_mixart_reset_board(mgr);
1066 		snd_printdd("reset miXart !\n");
1067 	}
1068 
1069 	/* release the i/o ports */
1070 	for (i = 0; i < 2; i++) {
1071 		if (mgr->mem[i].virt)
1072 			iounmap(mgr->mem[i].virt);
1073 	}
1074 	pci_release_regions(mgr->pci);
1075 
1076 	/* free flowarray */
1077 	if(mgr->flowinfo.area) {
1078 		snd_dma_free_pages(&mgr->flowinfo);
1079 		mgr->flowinfo.area = NULL;
1080 	}
1081 	/* free bufferarray */
1082 	if(mgr->bufferinfo.area) {
1083 		snd_dma_free_pages(&mgr->bufferinfo);
1084 		mgr->bufferinfo.area = NULL;
1085 	}
1086 
1087 	pci_disable_device(mgr->pci);
1088 	kfree(mgr);
1089 	return 0;
1090 }
1091 
1092 /*
1093  * proc interface
1094  */
1095 static long long snd_mixart_BA0_llseek(snd_info_entry_t *entry,
1096 				       void *private_file_data,
1097 				       struct file *file,
1098 				       long long offset,
1099 				       int orig)
1100 {
1101 	offset = offset & ~3; /* 4 bytes aligned */
1102 
1103 	switch(orig) {
1104 	case 0:  /* SEEK_SET */
1105 		file->f_pos = offset;
1106 		break;
1107 	case 1:  /* SEEK_CUR */
1108 		file->f_pos += offset;
1109 		break;
1110 	case 2:  /* SEEK_END, offset is negative */
1111 		file->f_pos = MIXART_BA0_SIZE + offset;
1112 		break;
1113 	default:
1114 		return -EINVAL;
1115 	}
1116 	if(file->f_pos > MIXART_BA0_SIZE)
1117 		file->f_pos = MIXART_BA0_SIZE;
1118 	return file->f_pos;
1119 }
1120 
1121 static long long snd_mixart_BA1_llseek(snd_info_entry_t *entry,
1122 				       void *private_file_data,
1123 				       struct file *file,
1124 				       long long offset,
1125 				       int orig)
1126 {
1127 	offset = offset & ~3; /* 4 bytes aligned */
1128 
1129 	switch(orig) {
1130 	case 0:  /* SEEK_SET */
1131 		file->f_pos = offset;
1132 		break;
1133 	case 1:  /* SEEK_CUR */
1134 		file->f_pos += offset;
1135 		break;
1136 	case 2: /* SEEK_END, offset is negative */
1137 		file->f_pos = MIXART_BA1_SIZE + offset;
1138 		break;
1139 	default:
1140 		return -EINVAL;
1141 	}
1142 	if(file->f_pos > MIXART_BA1_SIZE)
1143 		file->f_pos = MIXART_BA1_SIZE;
1144 	return file->f_pos;
1145 }
1146 
1147 /*
1148   mixart_BA0 proc interface for BAR 0 - read callback
1149  */
1150 static long snd_mixart_BA0_read(snd_info_entry_t *entry, void *file_private_data,
1151 				struct file *file, char __user *buf,
1152 				unsigned long count, unsigned long pos)
1153 {
1154 	mixart_mgr_t *mgr = entry->private_data;
1155 
1156 	count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1157 	if(count <= 0)
1158 		return 0;
1159 	if(pos + count > MIXART_BA0_SIZE)
1160 		count = (long)(MIXART_BA0_SIZE - pos);
1161 	if(copy_to_user_fromio(buf, MIXART_MEM( mgr, pos ), count))
1162 		return -EFAULT;
1163 	return count;
1164 }
1165 
1166 /*
1167   mixart_BA1 proc interface for BAR 1 - read callback
1168  */
1169 static long snd_mixart_BA1_read(snd_info_entry_t *entry, void *file_private_data,
1170 				struct file *file, char __user *buf,
1171 				unsigned long count, unsigned long pos)
1172 {
1173 	mixart_mgr_t *mgr = entry->private_data;
1174 
1175 	count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1176 	if(count <= 0)
1177 		return 0;
1178 	if(pos + count > MIXART_BA1_SIZE)
1179 		count = (long)(MIXART_BA1_SIZE - pos);
1180 	if(copy_to_user_fromio(buf, MIXART_REG( mgr, pos ), count))
1181 		return -EFAULT;
1182 	return count;
1183 }
1184 
1185 static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = {
1186 	.read   = snd_mixart_BA0_read,
1187 	.llseek = snd_mixart_BA0_llseek
1188 };
1189 
1190 static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = {
1191 	.read   = snd_mixart_BA1_read,
1192 	.llseek = snd_mixart_BA1_llseek
1193 };
1194 
1195 
1196 static void snd_mixart_proc_read(snd_info_entry_t *entry,
1197                                  snd_info_buffer_t * buffer)
1198 {
1199 	mixart_t *chip = entry->private_data;
1200 	u32 ref;
1201 
1202 	snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx);
1203 
1204 	/* stats available when embedded OS is running */
1205 	if (chip->mgr->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) {
1206 		snd_iprintf(buffer, "- hardware -\n");
1207 		switch (chip->mgr->board_type ) {
1208 		case MIXART_DAUGHTER_TYPE_NONE     : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break;
1209 		case MIXART_DAUGHTER_TYPE_AES      : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break;
1210 		case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break;
1211 		default:                             snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break;
1212 		}
1213 
1214 		snd_iprintf(buffer, "- system load -\n");
1215 
1216 		/* get perf reference */
1217 
1218 		ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET));
1219 
1220 		if (ref) {
1221 			u32 mailbox   = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref;
1222 			u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref;
1223 			u32 interr    = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref;
1224 
1225 			snd_iprintf(buffer, "\tstreaming          : %d\n", streaming);
1226 			snd_iprintf(buffer, "\tmailbox            : %d\n", mailbox);
1227 			snd_iprintf(buffer, "\tinterrups handling : %d\n\n", interr);
1228 		}
1229 	} /* endif elf loaded */
1230 }
1231 
1232 static void __devinit snd_mixart_proc_init(mixart_t *chip)
1233 {
1234 	snd_info_entry_t *entry;
1235 
1236 	/* text interface to read perf and temp meters */
1237 	if (! snd_card_proc_new(chip->card, "board_info", &entry)) {
1238 		entry->private_data = chip;
1239 		entry->c.text.read_size = 1024;
1240 		entry->c.text.read = snd_mixart_proc_read;
1241 	}
1242 
1243 	if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
1244 		entry->content = SNDRV_INFO_CONTENT_DATA;
1245 		entry->private_data = chip->mgr;
1246 		entry->c.ops = &snd_mixart_proc_ops_BA0;
1247 		entry->size = MIXART_BA0_SIZE;
1248 	}
1249 	if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
1250 		entry->content = SNDRV_INFO_CONTENT_DATA;
1251 		entry->private_data = chip->mgr;
1252 		entry->c.ops = &snd_mixart_proc_ops_BA1;
1253 		entry->size = MIXART_BA1_SIZE;
1254 	}
1255 }
1256 /* end of proc interface */
1257 
1258 
1259 /*
1260  *    probe function - creates the card manager
1261  */
1262 static int __devinit snd_mixart_probe(struct pci_dev *pci,
1263 				      const struct pci_device_id *pci_id)
1264 {
1265 	static int dev;
1266 	mixart_mgr_t *mgr;
1267 	unsigned int i;
1268 	int err;
1269 	size_t size;
1270 
1271 	/*
1272 	 */
1273 	if (dev >= SNDRV_CARDS)
1274 		return -ENODEV;
1275 	if (! enable[dev]) {
1276 		dev++;
1277 		return -ENOENT;
1278 	}
1279 
1280 	/* enable PCI device */
1281 	if ((err = pci_enable_device(pci)) < 0)
1282 		return err;
1283 	pci_set_master(pci);
1284 
1285 	/* check if we can restrict PCI DMA transfers to 32 bits */
1286 	if (pci_set_dma_mask(pci, 0xffffffff) < 0) {
1287 		snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
1288 		pci_disable_device(pci);
1289 		return -ENXIO;
1290 	}
1291 
1292 	/*
1293 	 */
1294 	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
1295 	if (! mgr) {
1296 		pci_disable_device(pci);
1297 		return -ENOMEM;
1298 	}
1299 
1300 	mgr->pci = pci;
1301 	mgr->irq = -1;
1302 
1303 	/* resource assignment */
1304 	if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
1305 		kfree(mgr);
1306 		pci_disable_device(pci);
1307 		return err;
1308 	}
1309 	for (i = 0; i < 2; i++) {
1310 		mgr->mem[i].phys = pci_resource_start(pci, i);
1311 		mgr->mem[i].virt = ioremap_nocache(mgr->mem[i].phys,
1312 						   pci_resource_len(pci, i));
1313 	}
1314 
1315 	if (request_irq(pci->irq, snd_mixart_interrupt, SA_INTERRUPT|SA_SHIRQ, CARD_NAME, (void *)mgr)) {
1316 		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1317 		snd_mixart_free(mgr);
1318 		return -EBUSY;
1319 	}
1320 	mgr->irq = pci->irq;
1321 
1322 	sprintf(mgr->shortname, "Digigram miXart");
1323 	sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
1324 
1325 	/* ISR spinlock  */
1326 	spin_lock_init(&mgr->lock);
1327 
1328 	/* init mailbox  */
1329 	mgr->msg_fifo_readptr = 0;
1330 	mgr->msg_fifo_writeptr = 0;
1331 
1332 	spin_lock_init(&mgr->msg_lock);
1333 	init_MUTEX(&mgr->msg_mutex);
1334 	init_waitqueue_head(&mgr->msg_sleep);
1335 	atomic_set(&mgr->msg_processed, 0);
1336 
1337 	/* init setup mutex*/
1338 	init_MUTEX(&mgr->setup_mutex);
1339 
1340 	/* init message taslket */
1341 	tasklet_init( &mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
1342 
1343 	/* card assignment */
1344 	mgr->num_cards = MIXART_MAX_CARDS; /* 4  FIXME: configurable? */
1345 	for (i = 0; i < mgr->num_cards; i++) {
1346 		snd_card_t *card;
1347 		char tmpid[16];
1348 		int idx;
1349 
1350 		if (index[dev] < 0)
1351 			idx = index[dev];
1352 		else
1353 			idx = index[dev] + i;
1354 		snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
1355 		card = snd_card_new(idx, tmpid, THIS_MODULE, 0);
1356 
1357 		if (! card) {
1358 			snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
1359 			snd_mixart_free(mgr);
1360 			return -ENOMEM;
1361 		}
1362 
1363 		strcpy(card->driver, CARD_NAME);
1364 		sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
1365 		sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
1366 
1367 		if ((err = snd_mixart_create(mgr, card, i)) < 0) {
1368 			snd_mixart_free(mgr);
1369 			return err;
1370 		}
1371 
1372 		if(i==0) {
1373 			/* init proc interface only for chip0 */
1374 			snd_mixart_proc_init(mgr->chip[i]);
1375 		}
1376 
1377 		if ((err = snd_card_register(card)) < 0) {
1378 			snd_mixart_free(mgr);
1379 			return err;
1380 		}
1381 	}
1382 
1383 	/* init firmware status (mgr->dsp_loaded reset in hwdep_new) */
1384 	mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
1385 
1386 	/* create array of streaminfo */
1387 	size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * sizeof(mixart_flowinfo_t)) );
1388 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1389 				size, &mgr->flowinfo) < 0) {
1390 		snd_mixart_free(mgr);
1391 		return -ENOMEM;
1392 	}
1393 	/* init streaminfo_array */
1394 	memset(mgr->flowinfo.area, 0, size);
1395 
1396 	/* create array of bufferinfo */
1397 	size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * sizeof(mixart_bufferinfo_t)) );
1398 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1399 				size, &mgr->bufferinfo) < 0) {
1400 		snd_mixart_free(mgr);
1401 		return -ENOMEM;
1402 	}
1403 	/* init bufferinfo_array */
1404 	memset(mgr->bufferinfo.area, 0, size);
1405 
1406 	/* set up firmware */
1407 	err = snd_mixart_setup_firmware(mgr);
1408 	if (err < 0) {
1409 		snd_mixart_free(mgr);
1410 		return err;
1411 	}
1412 
1413 	pci_set_drvdata(pci, mgr);
1414 	dev++;
1415 	return 0;
1416 }
1417 
1418 static void __devexit snd_mixart_remove(struct pci_dev *pci)
1419 {
1420 	snd_mixart_free(pci_get_drvdata(pci));
1421 	pci_set_drvdata(pci, NULL);
1422 }
1423 
1424 static struct pci_driver driver = {
1425 	.name = "Digigram miXart",
1426 	.id_table = snd_mixart_ids,
1427 	.probe = snd_mixart_probe,
1428 	.remove = __devexit_p(snd_mixart_remove),
1429 };
1430 
1431 static int __init alsa_card_mixart_init(void)
1432 {
1433 	return pci_register_driver(&driver);
1434 }
1435 
1436 static void __exit alsa_card_mixart_exit(void)
1437 {
1438 	pci_unregister_driver(&driver);
1439 }
1440 
1441 module_init(alsa_card_mixart_init)
1442 module_exit(alsa_card_mixart_exit)
1443