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