Linux Audio

Check our new training course

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