2 * Driver for Digigram miXart soundcards
4 * main file with alsa callbacks
6 * Copyright (c) 2003 by Digigram <alsa@digigram.com>
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.
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.
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
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/moduleparam.h>
29 #include <linux/mutex.h>
31 #include <sound/core.h>
32 #include <sound/initval.h>
33 #include <sound/info.h>
34 #include <sound/control.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
38 #include "mixart_hwdep.h"
39 #include "mixart_core.h"
40 #include "mixart_mixer.h"
42 #define CARD_NAME "miXart"
44 MODULE_AUTHOR("Digigram <alsa@digigram.com>");
45 MODULE_DESCRIPTION("Digigram " CARD_NAME);
46 MODULE_LICENSE("GPL");
47 MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");
49 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
50 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
51 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
53 module_param_array(index, int, NULL, 0444);
54 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
55 module_param_array(id, charp, NULL, 0444);
56 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
57 module_param_array(enable, bool, NULL, 0444);
58 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
63 static struct pci_device_id snd_mixart_ids[] = {
64 { 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
68 MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
71 static int mixart_set_pipe_state(struct mixart_mgr *mgr,
72 struct mixart_pipe *pipe, int start)
74 struct mixart_group_state_req group_state;
75 struct mixart_group_state_resp group_state_resp;
76 struct mixart_msg request;
80 switch(pipe->status) {
83 if(start) return 0; /* already started */
86 if(!start) return 0; /* already stopped */
89 snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
90 return -EINVAL; /* function called with wrong pipe status */
93 system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
95 /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
97 request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
98 request.uid = (struct mixart_uid){0,0};
99 request.data = &system_msg_uid;
100 request.size = sizeof(system_msg_uid);
102 err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
104 snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
108 /* start or stop the pipe (1 pipe) */
110 memset(&group_state, 0, sizeof(group_state));
111 group_state.pipe_count = 1;
112 group_state.pipe_uid[0] = pipe->group_uid;
115 request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
117 request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
119 request.uid = pipe->group_uid; /*(struct mixart_uid){0,0};*/
120 request.data = &group_state;
121 request.size = sizeof(group_state);
123 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
124 if (err < 0 || group_state_resp.txx_status != 0) {
125 snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
132 group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
134 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
135 if (err < 0 || group_state_resp.txx_status != 0) {
136 snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
140 /* in case of start send a synchro top */
142 request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
143 request.uid = (struct mixart_uid){0,0};
147 err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
148 if (err < 0 || stat != 0) {
149 snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
153 pipe->status = PIPE_RUNNING;
156 pipe->status = PIPE_STOPPED;
162 static int mixart_set_clock(struct mixart_mgr *mgr,
163 struct mixart_pipe *pipe, unsigned int rate)
165 struct mixart_msg request;
166 struct mixart_clock_properties clock_properties;
167 struct mixart_clock_properties_resp clock_prop_resp;
170 switch(pipe->status) {
178 return 0; /* nothing to do */
180 snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
185 memset(&clock_properties, 0, sizeof(clock_properties));
186 clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
187 clock_properties.clock_mode = CM_STANDALONE;
188 clock_properties.frequency = rate;
189 clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
190 clock_properties.uid_caller[0] = pipe->group_uid;
192 snd_printdd("mixart_set_clock to %d kHz\n", rate);
194 request.message_id = MSG_CLOCK_SET_PROPERTIES;
195 request.uid = mgr->uid_console_manager;
196 request.data = &clock_properties;
197 request.size = sizeof(clock_properties);
199 err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
200 if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
201 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);
205 if(rate) pipe->status = PIPE_CLOCK_SET;
206 else pipe->status = PIPE_RUNNING;
213 * Allocate or reference output pipe for analog IOs (pcmp0/1)
216 snd_mixart_add_ref_pipe(struct snd_mixart *chip, int pcm_number, int capture,
220 struct mixart_pipe *pipe;
221 struct mixart_msg request;
224 if (pcm_number == MIXART_PCM_ANALOG) {
225 pipe = &(chip->pipe_in_ana); /* analog inputs */
227 pipe = &(chip->pipe_in_dig); /* digital inputs */
229 request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
230 stream_count = MIXART_CAPTURE_STREAMS;
232 if (pcm_number == MIXART_PCM_ANALOG) {
233 pipe = &(chip->pipe_out_ana); /* analog outputs */
235 pipe = &(chip->pipe_out_dig); /* digital outputs */
237 request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
238 stream_count = MIXART_PLAYBACK_STREAMS;
241 /* a new stream is opened and there are already all streams in use */
242 if( (monitoring == 0) && (pipe->references >= stream_count) ) {
246 /* pipe is not yet defined */
247 if( pipe->status == PIPE_UNDEFINED ) {
250 struct mixart_streaming_group_req sgroup_req;
251 struct mixart_streaming_group sgroup_resp;
254 snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
256 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
260 request.uid = (struct mixart_uid){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */
261 request.data = &buf->sgroup_req;
262 request.size = sizeof(buf->sgroup_req);
264 memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
266 buf->sgroup_req.stream_count = stream_count;
267 buf->sgroup_req.channel_count = 2;
268 buf->sgroup_req.latency = 256;
269 buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */
271 for (i=0; i<stream_count; i++) {
273 struct mixart_flowinfo *flowinfo;
274 struct mixart_bufferinfo *bufferinfo;
276 /* we don't yet know the format, so config 16 bit pcm audio for instance */
277 buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
278 buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
279 buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
281 /* find the right bufferinfo_array */
282 j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
283 if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
285 buf->sgroup_req.flow_entry[i] = j;
287 flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
288 flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(struct mixart_bufferinfo));
289 flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */
291 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
292 bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */
293 bufferinfo[j].available_length = 0; /* buffer is not yet allocated */
295 /* construct the identifier of the stream buffer received in the interrupts ! */
296 bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
298 bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
302 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
303 if((err < 0) || (buf->sgroup_resp.status != 0)) {
304 snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
309 pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */
310 pipe->stream_count = buf->sgroup_resp.stream_count;
311 /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
313 pipe->status = PIPE_STOPPED;
317 if(monitoring) pipe->monitoring = 1;
318 else pipe->references++;
324 int snd_mixart_kill_ref_pipe(struct mixart_mgr *mgr,
325 struct mixart_pipe *pipe, int monitoring)
329 if(pipe->status == PIPE_UNDEFINED)
333 pipe->monitoring = 0;
337 if((pipe->references <= 0) && (pipe->monitoring == 0)) {
339 struct mixart_msg request;
340 struct mixart_delete_group_resp delete_resp;
342 /* release the clock */
343 err = mixart_set_clock( mgr, pipe, 0);
345 snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
349 err = mixart_set_pipe_state(mgr, pipe, 0);
351 snd_printk(KERN_ERR "error stopping pipe!\n");
354 request.message_id = MSG_STREAM_DELETE_GROUP;
355 request.uid = (struct mixart_uid){0,0};
356 request.data = &pipe->group_uid; /* the streaming group ! */
357 request.size = sizeof(pipe->group_uid);
359 /* delete the pipe */
360 err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
361 if ((err < 0) || (delete_resp.status != 0)) {
362 snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
365 pipe->group_uid = (struct mixart_uid){0,0};
366 pipe->stream_count = 0;
367 pipe->status = PIPE_UNDEFINED;
373 static int mixart_set_stream_state(struct mixart_stream *stream, int start)
375 struct snd_mixart *chip;
376 struct mixart_stream_state_req stream_state_req;
377 struct mixart_msg request;
379 if(!stream->substream)
382 memset(&stream_state_req, 0, sizeof(stream_state_req));
383 stream_state_req.stream_count = 1;
384 stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
385 stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
387 if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
390 request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
392 request.uid = (struct mixart_uid){0,0};
393 request.data = &stream_state_req;
394 request.size = sizeof(stream_state_req);
396 stream->abs_period_elapsed = 0; /* reset stream pos */
397 stream->buf_periods = 0;
398 stream->buf_period_frag = 0;
400 chip = snd_pcm_substream_chip(stream->substream);
402 return snd_mixart_send_msg_nonblock(chip->mgr, &request);
409 static int snd_mixart_trigger(struct snd_pcm_substream *subs, int cmd)
411 struct mixart_stream *stream = subs->runtime->private_data;
414 case SNDRV_PCM_TRIGGER_START:
416 snd_printdd("SNDRV_PCM_TRIGGER_START\n");
419 if( mixart_set_stream_state(stream, 1) )
422 stream->status = MIXART_STREAM_STATUS_RUNNING;
425 case SNDRV_PCM_TRIGGER_STOP:
428 if( mixart_set_stream_state(stream, 0) )
431 stream->status = MIXART_STREAM_STATUS_OPEN;
433 snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
437 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
439 stream->status = MIXART_STREAM_STATUS_PAUSE;
440 snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
442 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
444 stream->status = MIXART_STREAM_STATUS_RUNNING;
445 snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
453 static int mixart_sync_nonblock_events(struct mixart_mgr *mgr)
455 unsigned long timeout = jiffies + HZ;
456 while (atomic_read(&mgr->msg_processed) > 0) {
457 if (time_after(jiffies, timeout)) {
458 snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
461 schedule_timeout_uninterruptible(1);
467 * prepare callback for all pcms
469 static int snd_mixart_prepare(struct snd_pcm_substream *subs)
471 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
472 struct mixart_stream *stream = subs->runtime->private_data;
474 /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
476 snd_printdd("snd_mixart_prepare\n");
478 mixart_sync_nonblock_events(chip->mgr);
480 /* only the first stream can choose the sample rate */
481 /* the further opened streams will be limited to its frequency (see open) */
482 if(chip->mgr->ref_count_rate == 1)
483 chip->mgr->sample_rate = subs->runtime->rate;
485 /* set the clock only once (first stream) on the same pipe */
486 if(stream->pipe->references == 1) {
487 if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
495 static int mixart_set_format(struct mixart_stream *stream, snd_pcm_format_t format)
498 struct snd_mixart *chip;
499 struct mixart_msg request;
500 struct mixart_stream_param_desc stream_param;
501 struct mixart_return_uid resp;
503 chip = snd_pcm_substream_chip(stream->substream);
505 memset(&stream_param, 0, sizeof(stream_param));
507 stream_param.coding_type = CT_LINEAR;
508 stream_param.number_of_channel = stream->channels;
510 stream_param.sampling_freq = chip->mgr->sample_rate;
511 if(stream_param.sampling_freq == 0)
512 stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
515 case SNDRV_PCM_FORMAT_U8:
516 stream_param.sample_type = ST_INTEGER_8;
517 stream_param.sample_size = 8;
519 case SNDRV_PCM_FORMAT_S16_LE:
520 stream_param.sample_type = ST_INTEGER_16LE;
521 stream_param.sample_size = 16;
523 case SNDRV_PCM_FORMAT_S16_BE:
524 stream_param.sample_type = ST_INTEGER_16BE;
525 stream_param.sample_size = 16;
527 case SNDRV_PCM_FORMAT_S24_3LE:
528 stream_param.sample_type = ST_INTEGER_24LE;
529 stream_param.sample_size = 24;
531 case SNDRV_PCM_FORMAT_S24_3BE:
532 stream_param.sample_type = ST_INTEGER_24BE;
533 stream_param.sample_size = 24;
535 case SNDRV_PCM_FORMAT_FLOAT_LE:
536 stream_param.sample_type = ST_FLOATING_POINT_32LE;
537 stream_param.sample_size = 32;
539 case SNDRV_PCM_FORMAT_FLOAT_BE:
540 stream_param.sample_type = ST_FLOATING_POINT_32BE;
541 stream_param.sample_size = 32;
544 snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
548 snd_printdd("set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
549 stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
551 /* TODO: what else to configure ? */
552 /* stream_param.samples_per_frame = 2; */
553 /* stream_param.bytes_per_frame = 4; */
554 /* stream_param.bytes_per_sample = 2; */
556 stream_param.pipe_count = 1; /* set to 1 */
557 stream_param.stream_count = 1; /* set to 1 */
558 stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
559 stream_param.stream_desc[0].stream_idx = stream->substream->number;
561 request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
562 request.uid = (struct mixart_uid){0,0};
563 request.data = &stream_param;
564 request.size = sizeof(stream_param);
566 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
567 if((err < 0) || resp.error_code) {
568 snd_printk(KERN_ERR "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", err, resp.error_code);
576 * HW_PARAMS callback for all pcms
578 static int snd_mixart_hw_params(struct snd_pcm_substream *subs,
579 struct snd_pcm_hw_params *hw)
581 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
582 struct mixart_mgr *mgr = chip->mgr;
583 struct mixart_stream *stream = subs->runtime->private_data;
584 snd_pcm_format_t format;
588 /* set up channels */
589 channels = params_channels(hw);
591 /* set up format for the stream */
592 format = params_format(hw);
594 mutex_lock(&mgr->setup_mutex);
596 /* update the stream levels */
597 if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
598 int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
599 if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
600 mixart_update_playback_stream_level(chip, is_aes, subs->number);
602 mixart_update_capture_stream_level( chip, is_aes);
605 stream->channels = channels;
607 /* set the format to the board */
608 err = mixart_set_format(stream, format);
610 mutex_unlock(&mgr->setup_mutex);
614 /* allocate buffer */
615 err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
618 struct mixart_bufferinfo *bufferinfo;
619 int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number;
620 if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) {
621 i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */
624 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
625 bufferinfo[i].buffer_address = subs->runtime->dma_addr;
626 bufferinfo[i].available_length = subs->runtime->dma_bytes;
627 /* bufferinfo[i].buffer_id is already defined */
629 snd_printdd("snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", i,
630 bufferinfo[i].buffer_address,
631 bufferinfo[i].available_length,
634 mutex_unlock(&mgr->setup_mutex);
639 static int snd_mixart_hw_free(struct snd_pcm_substream *subs)
641 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
642 snd_pcm_lib_free_pages(subs);
643 mixart_sync_nonblock_events(chip->mgr);
650 * TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
652 static struct snd_pcm_hardware snd_mixart_analog_caps =
654 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
655 SNDRV_PCM_INFO_MMAP_VALID |
656 SNDRV_PCM_INFO_PAUSE),
657 .formats = ( SNDRV_PCM_FMTBIT_U8 |
658 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
659 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
660 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
661 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
666 .buffer_bytes_max = (32*1024),
667 .period_bytes_min = 256, /* 256 frames U8 mono*/
668 .period_bytes_max = (16*1024),
670 .periods_max = (32*1024/256),
673 static struct snd_pcm_hardware snd_mixart_digital_caps =
675 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
676 SNDRV_PCM_INFO_MMAP_VALID |
677 SNDRV_PCM_INFO_PAUSE),
678 .formats = ( SNDRV_PCM_FMTBIT_U8 |
679 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
680 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
681 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
682 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
687 .buffer_bytes_max = (32*1024),
688 .period_bytes_min = 256, /* 256 frames U8 mono*/
689 .period_bytes_max = (16*1024),
691 .periods_max = (32*1024/256),
695 static int snd_mixart_playback_open(struct snd_pcm_substream *subs)
697 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
698 struct mixart_mgr *mgr = chip->mgr;
699 struct snd_pcm_runtime *runtime = subs->runtime;
700 struct snd_pcm *pcm = subs->pcm;
701 struct mixart_stream *stream;
702 struct mixart_pipe *pipe;
706 mutex_lock(&mgr->setup_mutex);
708 if ( pcm == chip->pcm ) {
709 pcm_number = MIXART_PCM_ANALOG;
710 runtime->hw = snd_mixart_analog_caps;
712 snd_BUG_ON(pcm != chip->pcm_dig);
713 pcm_number = MIXART_PCM_DIGITAL;
714 runtime->hw = snd_mixart_digital_caps;
716 snd_printdd("snd_mixart_playback_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
718 /* get stream info */
719 stream = &(chip->playback_stream[pcm_number][subs->number]);
721 if (stream->status != MIXART_STREAM_STATUS_FREE){
723 snd_printk(KERN_ERR "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
728 /* get pipe pointer (out pipe) */
729 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
736 /* start the pipe if necessary */
737 err = mixart_set_pipe_state(chip->mgr, pipe, 1);
739 snd_printk(KERN_ERR "error starting pipe!\n");
740 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
746 stream->pcm_number = pcm_number;
747 stream->status = MIXART_STREAM_STATUS_OPEN;
748 stream->substream = subs;
749 stream->channels = 0; /* not configured yet */
751 runtime->private_data = stream;
753 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
754 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
756 /* if a sample rate is already used, another stream cannot change */
757 if(mgr->ref_count_rate++) {
758 if(mgr->sample_rate) {
759 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
764 mutex_unlock(&mgr->setup_mutex);
770 static int snd_mixart_capture_open(struct snd_pcm_substream *subs)
772 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
773 struct mixart_mgr *mgr = chip->mgr;
774 struct snd_pcm_runtime *runtime = subs->runtime;
775 struct snd_pcm *pcm = subs->pcm;
776 struct mixart_stream *stream;
777 struct mixart_pipe *pipe;
781 mutex_lock(&mgr->setup_mutex);
783 if ( pcm == chip->pcm ) {
784 pcm_number = MIXART_PCM_ANALOG;
785 runtime->hw = snd_mixart_analog_caps;
787 snd_BUG_ON(pcm != chip->pcm_dig);
788 pcm_number = MIXART_PCM_DIGITAL;
789 runtime->hw = snd_mixart_digital_caps;
792 runtime->hw.channels_min = 2; /* for instance, no mono */
794 snd_printdd("snd_mixart_capture_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
796 /* get stream info */
797 stream = &(chip->capture_stream[pcm_number]);
799 if (stream->status != MIXART_STREAM_STATUS_FREE){
801 snd_printk(KERN_ERR "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
806 /* get pipe pointer (in pipe) */
807 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
814 /* start the pipe if necessary */
815 err = mixart_set_pipe_state(chip->mgr, pipe, 1);
817 snd_printk(KERN_ERR "error starting pipe!\n");
818 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
824 stream->pcm_number = pcm_number;
825 stream->status = MIXART_STREAM_STATUS_OPEN;
826 stream->substream = subs;
827 stream->channels = 0; /* not configured yet */
829 runtime->private_data = stream;
831 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
832 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
834 /* if a sample rate is already used, another stream cannot change */
835 if(mgr->ref_count_rate++) {
836 if(mgr->sample_rate) {
837 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
842 mutex_unlock(&mgr->setup_mutex);
849 static int snd_mixart_close(struct snd_pcm_substream *subs)
851 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
852 struct mixart_mgr *mgr = chip->mgr;
853 struct mixart_stream *stream = subs->runtime->private_data;
855 mutex_lock(&mgr->setup_mutex);
857 snd_printdd("snd_mixart_close C%d/P%d/Sub%d\n", chip->chip_idx, stream->pcm_number, subs->number);
859 /* sample rate released */
860 if(--mgr->ref_count_rate == 0) {
861 mgr->sample_rate = 0;
865 if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
867 snd_printk(KERN_ERR "error snd_mixart_kill_ref_pipe C%dP%d\n", chip->chip_idx, stream->pcm_number);
871 stream->status = MIXART_STREAM_STATUS_FREE;
872 stream->substream = NULL;
874 mutex_unlock(&mgr->setup_mutex);
879 static snd_pcm_uframes_t snd_mixart_stream_pointer(struct snd_pcm_substream *subs)
881 struct snd_pcm_runtime *runtime = subs->runtime;
882 struct mixart_stream *stream = runtime->private_data;
884 return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
889 static struct snd_pcm_ops snd_mixart_playback_ops = {
890 .open = snd_mixart_playback_open,
891 .close = snd_mixart_close,
892 .ioctl = snd_pcm_lib_ioctl,
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,
900 static struct snd_pcm_ops snd_mixart_capture_ops = {
901 .open = snd_mixart_capture_open,
902 .close = snd_mixart_close,
903 .ioctl = snd_pcm_lib_ioctl,
904 .prepare = snd_mixart_prepare,
905 .hw_params = snd_mixart_hw_params,
906 .hw_free = snd_mixart_hw_free,
907 .trigger = snd_mixart_trigger,
908 .pointer = snd_mixart_stream_pointer,
911 static void preallocate_buffers(struct snd_mixart *chip, struct snd_pcm *pcm)
914 struct snd_pcm_substream *subs;
917 for (stream = 0; stream < 2; stream++) {
919 for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
920 /* set up the unique device id with the chip index */
921 subs->dma_device.id = subs->pcm->device << 16 |
922 subs->stream << 8 | (subs->number + 1) |
923 (chip->chip_idx + 1) << 24;
926 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
927 snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
932 static int snd_mixart_pcm_analog(struct snd_mixart *chip)
938 sprintf(name, "miXart analog %d", chip->chip_idx);
939 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
940 MIXART_PLAYBACK_STREAMS,
941 MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
942 snd_printk(KERN_ERR "cannot create the analog pcm %d\n", chip->chip_idx);
946 pcm->private_data = chip;
948 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
949 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
952 strcpy(pcm->name, name);
954 preallocate_buffers(chip, pcm);
963 static int snd_mixart_pcm_digital(struct snd_mixart *chip)
969 sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
970 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
971 MIXART_PLAYBACK_STREAMS,
972 MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
973 snd_printk(KERN_ERR "cannot create the digital pcm %d\n", chip->chip_idx);
977 pcm->private_data = chip;
979 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
980 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
983 strcpy(pcm->name, name);
985 preallocate_buffers(chip, pcm);
991 static int snd_mixart_chip_free(struct snd_mixart *chip)
997 static int snd_mixart_chip_dev_free(struct snd_device *device)
999 struct snd_mixart *chip = device->device_data;
1000 return snd_mixart_chip_free(chip);
1006 static int __devinit snd_mixart_create(struct mixart_mgr *mgr, struct snd_card *card, int idx)
1009 struct snd_mixart *chip;
1010 static struct snd_device_ops ops = {
1011 .dev_free = snd_mixart_chip_dev_free,
1014 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1016 snd_printk(KERN_ERR "cannot allocate chip\n");
1021 chip->chip_idx = idx;
1024 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1025 snd_mixart_chip_free(chip);
1029 mgr->chip[idx] = chip;
1030 snd_card_set_dev(card, &mgr->pci->dev);
1035 int snd_mixart_create_pcm(struct snd_mixart* chip)
1039 err = snd_mixart_pcm_analog(chip);
1043 if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
1045 err = snd_mixart_pcm_digital(chip);
1054 * release all the cards assigned to a manager instance
1056 static int snd_mixart_free(struct mixart_mgr *mgr)
1060 for (i = 0; i < mgr->num_cards; i++) {
1062 snd_card_free(mgr->chip[i]->card);
1066 snd_mixart_exit_mailbox(mgr);
1070 free_irq(mgr->irq, mgr);
1072 /* reset board if some firmware was loaded */
1073 if(mgr->dsp_loaded) {
1074 snd_mixart_reset_board(mgr);
1075 snd_printdd("reset miXart !\n");
1078 /* release the i/o ports */
1079 for (i = 0; i < 2; i++) {
1080 if (mgr->mem[i].virt)
1081 iounmap(mgr->mem[i].virt);
1083 pci_release_regions(mgr->pci);
1085 /* free flowarray */
1086 if(mgr->flowinfo.area) {
1087 snd_dma_free_pages(&mgr->flowinfo);
1088 mgr->flowinfo.area = NULL;
1090 /* free bufferarray */
1091 if(mgr->bufferinfo.area) {
1092 snd_dma_free_pages(&mgr->bufferinfo);
1093 mgr->bufferinfo.area = NULL;
1096 pci_disable_device(mgr->pci);
1104 static long long snd_mixart_BA0_llseek(struct snd_info_entry *entry,
1105 void *private_file_data,
1110 offset = offset & ~3; /* 4 bytes aligned */
1114 file->f_pos = offset;
1117 file->f_pos += offset;
1119 case SEEK_END: /* offset is negative */
1120 file->f_pos = MIXART_BA0_SIZE + offset;
1125 if(file->f_pos > MIXART_BA0_SIZE)
1126 file->f_pos = MIXART_BA0_SIZE;
1130 static long long snd_mixart_BA1_llseek(struct snd_info_entry *entry,
1131 void *private_file_data,
1136 offset = offset & ~3; /* 4 bytes aligned */
1140 file->f_pos = offset;
1143 file->f_pos += offset;
1145 case SEEK_END: /* offset is negative */
1146 file->f_pos = MIXART_BA1_SIZE + offset;
1151 if(file->f_pos > MIXART_BA1_SIZE)
1152 file->f_pos = MIXART_BA1_SIZE;
1157 mixart_BA0 proc interface for BAR 0 - read callback
1159 static long snd_mixart_BA0_read(struct snd_info_entry *entry, void *file_private_data,
1160 struct file *file, char __user *buf,
1161 unsigned long count, unsigned long pos)
1163 struct mixart_mgr *mgr = entry->private_data;
1165 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1168 if(pos + count > MIXART_BA0_SIZE)
1169 count = (long)(MIXART_BA0_SIZE - pos);
1170 if(copy_to_user_fromio(buf, MIXART_MEM( mgr, pos ), count))
1176 mixart_BA1 proc interface for BAR 1 - read callback
1178 static long snd_mixart_BA1_read(struct snd_info_entry *entry, void *file_private_data,
1179 struct file *file, char __user *buf,
1180 unsigned long count, unsigned long pos)
1182 struct mixart_mgr *mgr = entry->private_data;
1184 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1187 if(pos + count > MIXART_BA1_SIZE)
1188 count = (long)(MIXART_BA1_SIZE - pos);
1189 if(copy_to_user_fromio(buf, MIXART_REG( mgr, pos ), count))
1194 static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = {
1195 .read = snd_mixart_BA0_read,
1196 .llseek = snd_mixart_BA0_llseek
1199 static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = {
1200 .read = snd_mixart_BA1_read,
1201 .llseek = snd_mixart_BA1_llseek
1205 static void snd_mixart_proc_read(struct snd_info_entry *entry,
1206 struct snd_info_buffer *buffer)
1208 struct snd_mixart *chip = entry->private_data;
1211 snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx);
1213 /* stats available when embedded OS is running */
1214 if (chip->mgr->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) {
1215 snd_iprintf(buffer, "- hardware -\n");
1216 switch (chip->mgr->board_type ) {
1217 case MIXART_DAUGHTER_TYPE_NONE : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break;
1218 case MIXART_DAUGHTER_TYPE_AES : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break;
1219 case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break;
1220 default: snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break;
1223 snd_iprintf(buffer, "- system load -\n");
1225 /* get perf reference */
1227 ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET));
1230 u32 mailbox = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref;
1231 u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref;
1232 u32 interr = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref;
1234 snd_iprintf(buffer, "\tstreaming : %d\n", streaming);
1235 snd_iprintf(buffer, "\tmailbox : %d\n", mailbox);
1236 snd_iprintf(buffer, "\tinterrups handling : %d\n\n", interr);
1238 } /* endif elf loaded */
1241 static void __devinit snd_mixart_proc_init(struct snd_mixart *chip)
1243 struct snd_info_entry *entry;
1245 /* text interface to read perf and temp meters */
1246 if (! snd_card_proc_new(chip->card, "board_info", &entry)) {
1247 entry->private_data = chip;
1248 entry->c.text.read = snd_mixart_proc_read;
1251 if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
1252 entry->content = SNDRV_INFO_CONTENT_DATA;
1253 entry->private_data = chip->mgr;
1254 entry->c.ops = &snd_mixart_proc_ops_BA0;
1255 entry->size = MIXART_BA0_SIZE;
1257 if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
1258 entry->content = SNDRV_INFO_CONTENT_DATA;
1259 entry->private_data = chip->mgr;
1260 entry->c.ops = &snd_mixart_proc_ops_BA1;
1261 entry->size = MIXART_BA1_SIZE;
1264 /* end of proc interface */
1268 * probe function - creates the card manager
1270 static int __devinit snd_mixart_probe(struct pci_dev *pci,
1271 const struct pci_device_id *pci_id)
1274 struct mixart_mgr *mgr;
1281 if (dev >= SNDRV_CARDS)
1283 if (! enable[dev]) {
1288 /* enable PCI device */
1289 if ((err = pci_enable_device(pci)) < 0)
1291 pci_set_master(pci);
1293 /* check if we can restrict PCI DMA transfers to 32 bits */
1294 if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0) {
1295 snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
1296 pci_disable_device(pci);
1302 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
1304 pci_disable_device(pci);
1311 /* resource assignment */
1312 if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
1314 pci_disable_device(pci);
1317 for (i = 0; i < 2; i++) {
1318 mgr->mem[i].phys = pci_resource_start(pci, i);
1319 mgr->mem[i].virt = pci_ioremap_bar(pci, i);
1320 if (!mgr->mem[i].virt) {
1321 printk(KERN_ERR "unable to remap resource 0x%lx\n",
1323 snd_mixart_free(mgr);
1328 if (request_irq(pci->irq, snd_mixart_interrupt, IRQF_SHARED,
1330 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1331 snd_mixart_free(mgr);
1334 mgr->irq = pci->irq;
1336 sprintf(mgr->shortname, "Digigram miXart");
1337 sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
1340 spin_lock_init(&mgr->lock);
1343 mgr->msg_fifo_readptr = 0;
1344 mgr->msg_fifo_writeptr = 0;
1346 spin_lock_init(&mgr->msg_lock);
1347 mutex_init(&mgr->msg_mutex);
1348 init_waitqueue_head(&mgr->msg_sleep);
1349 atomic_set(&mgr->msg_processed, 0);
1351 /* init setup mutex*/
1352 mutex_init(&mgr->setup_mutex);
1354 /* init message taslket */
1355 tasklet_init(&mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
1357 /* card assignment */
1358 mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */
1359 for (i = 0; i < mgr->num_cards; i++) {
1360 struct snd_card *card;
1367 idx = index[dev] + i;
1368 snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
1369 err = snd_card_create(idx, tmpid, THIS_MODULE, 0, &card);
1372 snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
1373 snd_mixart_free(mgr);
1377 strcpy(card->driver, CARD_NAME);
1378 sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
1379 sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
1381 if ((err = snd_mixart_create(mgr, card, i)) < 0) {
1382 snd_card_free(card);
1383 snd_mixart_free(mgr);
1388 /* init proc interface only for chip0 */
1389 snd_mixart_proc_init(mgr->chip[i]);
1392 if ((err = snd_card_register(card)) < 0) {
1393 snd_mixart_free(mgr);
1398 /* init firmware status (mgr->dsp_loaded reset in hwdep_new) */
1399 mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
1401 /* create array of streaminfo */
1402 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
1403 sizeof(struct mixart_flowinfo)) );
1404 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1405 size, &mgr->flowinfo) < 0) {
1406 snd_mixart_free(mgr);
1409 /* init streaminfo_array */
1410 memset(mgr->flowinfo.area, 0, size);
1412 /* create array of bufferinfo */
1413 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
1414 sizeof(struct mixart_bufferinfo)) );
1415 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1416 size, &mgr->bufferinfo) < 0) {
1417 snd_mixart_free(mgr);
1420 /* init bufferinfo_array */
1421 memset(mgr->bufferinfo.area, 0, size);
1423 /* set up firmware */
1424 err = snd_mixart_setup_firmware(mgr);
1426 snd_mixart_free(mgr);
1430 pci_set_drvdata(pci, mgr);
1435 static void __devexit snd_mixart_remove(struct pci_dev *pci)
1437 snd_mixart_free(pci_get_drvdata(pci));
1438 pci_set_drvdata(pci, NULL);
1441 static struct pci_driver driver = {
1442 .name = "Digigram miXart",
1443 .id_table = snd_mixart_ids,
1444 .probe = snd_mixart_probe,
1445 .remove = __devexit_p(snd_mixart_remove),
1448 static int __init alsa_card_mixart_init(void)
1450 return pci_register_driver(&driver);
1453 static void __exit alsa_card_mixart_exit(void)
1455 pci_unregister_driver(&driver);
1458 module_init(alsa_card_mixart_init)
1459 module_exit(alsa_card_mixart_exit)